`import type` in TypeScript

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

4 thoughts on “`import type` in TypeScript