question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`import type` in TypeScript

See original GitHub issue

TypeScript 3.8 will support a new import type construct. While there’s already some logic for similar stuff (Flow has its own import type), TypeScript’s has a few extra restrictions.

  • import type can only be used on imports with named imports or with a default import - but not both.

    import type FooDefault, { Bar, Baz } from "module"; // error!
    
  • Even though import type creates no emit, an import type functionally shadows variables from the outer scope, and using these declarations in a value position that will be emitted causes an error.

    import type { RegExp } from "special-regexp-module";
    
    // Okay - RegExp won't be used at runtime here.
    declare class Foo extends RegExp { }
    
    // Okay - RegExp won't be used at runtime here.
    let r: typeof RegExp;
    
    // Error! RegExp is imported via import type and cannot be used as a value here.
    new RegExp()
    

See more at

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:42
  • Comments:22 (16 by maintainers)

github_iconTop GitHub Comments

2reactions
ExE-Bosscommented, Jan 17, 2020

I thought this was the TypeScript repository when I posted my previous comment.

2reactions
jridgewellcommented, Jan 14, 2020

Currently, we use a .type property for flow imports. It would be nice to keep them aligned.

The explorer suggests we use importKind: 'type' for type imports and importKind: 'value' for value imports.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Documentation - TypeScript 3.8
import type only imports declarations to be used for type annotations and declarations. It always gets fully erased, so there's no remnant of...
Read more >
Do I need to use the "import type" feature of TypeScript 3.8 ...
import type only imports declarations to be used for type annotations and declarations. It always gets fully erased, so there's no remnant of...
Read more >
Import a Type from Another file using TypeScript
To import a type from another file in TypeScript: ... Here is an example of exporting a type from a file called another-file.ts...
Read more >
Leveraging Type-Only imports and exports with TypeScript ...
Well, as the TS release notes explain, by importing an element using “import type”, it tells the compiler that the element is only...
Read more >
type-only imports — A new TypeScript feature that benefits ...
type -only imports — A new TypeScript feature that benefits Babel users · Adds static type checking to code you would traditionally write...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found