Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TS - type-only imports & exports #61

Open
jtwang7 opened this issue Mar 11, 2022 · 0 comments
Open

TS - type-only imports & exports #61

jtwang7 opened this issue Mar 11, 2022 · 0 comments

Comments

@jtwang7
Copy link
Owner

jtwang7 commented Mar 11, 2022

Content

TypeScript 3.8 adds a new syntax for type-only imports and exports.

import type { SomeThing } from "./some-module.ts";
export type { SomeThing };

⭕️ type-only import 不局限于 type 别名,也可以是 interface 接口

export interface MyInterface {
   // ...
}
import type {MyInterface} from './xxx.ts';

TypeScript 引入 type-only 导入导出的目的:
import type 只导入用于类型注释和声明的声明。它在编译完成后总是被完全擦除,因此在运行时不会有任何残留。
同样,导出类型仅提供可用于类型上下文的导出,并且也会从 TypeScript 的输出中删除。

❗️❗️❗️
type-only import 要么是 default 引入,要么是具名引入。
在这一点上与 ES6 module import 有所区分,ES6 可同时引入一个 default + 若干具名接口。

// Is only 'Foo' a type? Or every declaration in the import?
// We just give an error because it's not clear.
import type Foo, { Bar, Baz } from "some-module";
//     ~~~~~~~~~~~~~~~~~~~~~~
// error! A type-only import can specify a default import or named bindings, but not both.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant