Skip to content

Instantly share code, notes, and snippets.

@hmil
Created March 3, 2020 20:47
Show Gist options
  • Save hmil/ae1d07a48dd6bbc3ffbce910b41f7182 to your computer and use it in GitHub Desktop.
Save hmil/ae1d07a48dd6bbc3ffbce910b41f7182 to your computer and use it in GitHub Desktop.
TypeScript import stripping
export type MyType = { foo: string };
export interface MyInterface {
foo: string;
}
export class MyClass implements MyInterface {
constructor(private foo: string) { }
}
export enum MyEnum { CASE_1, CASE_2 }
// This import statement will be completely removed from the output JavaScript because none of the imported symbols
// are used for their actual JavaScript value
import { MyType, MyClass, MyInterface, MyEnum } from "./definitions";
const instance: MyInterface = getInstance<MyClass>("my-class");
const theType: MyType = { foo: 'abc' };
class MyOtherClass implements MyInterface {
member: MyEnum;
}
// This import statement will be transformed to: import { MyClass, MyEnum } from "./definitions";
// because MyClass and MyEnum are actually needed at runtime
import { MyType, MyClass, MyInterface, MyEnum } from "./definitions";
const instance: MyInterface = getInstance(MyClass); // MyClass constructor is actually used here
// types and interfaces are never retained. They do not exist in JavaScript.
const theType: MyType = { foo: 'abc' };
class MyOtherClass implements MyInterface{
member: MyEnum = MyEnum.CASE_1; // Value of CASE_1 is used here
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment