Created
March 3, 2020 20:47
-
-
Save hmil/ae1d07a48dd6bbc3ffbce910b41f7182 to your computer and use it in GitHub Desktop.
TypeScript import stripping
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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