Skip to content

Commit

Permalink
Type-Only Import Paths with TypeScript Implementation File Extension (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
gibbok committed Sep 7, 2023
1 parent 3fc89a3 commit 795eb8f
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
27 changes: 27 additions & 0 deletions README-zh_CN.md
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@
- [类型参数的可选方差注释](#类型参数的可选方差注释)
- [Symbol和模板字符串模式索引签名](#symbol和模板字符串模式索引签名)
- [satisfies操作符](#satisfies操作符)
- [仅类型导入和导出](#仅类型导入和导出)
<!-- markdownlint-enable MD004 -->

## 介绍
Expand Down Expand Up @@ -4733,3 +4734,29 @@ const user3 = {
user3.attributes?.map(console.log); // TypeScript 推断正确: string[]
user3.nickName; // TypeScript 推断正确: undefined
```

### 仅类型导入和导出

仅类型导入和导出允许您导入或导出类型,而无需导入或导出与这些类型关联的值或函数。 这对于减小捆绑包的大小很有用。

要使用仅类型导入,您可以使用“导入类型关键字”。

TypeScript 允许在仅类型导入中使用声明和实现文件扩展名(.ts、.mts、.cts 和 .tsx),无论“allowImportingTsExtensions”设置如何。

例如:

<!-- skip -->
```typescript
import type { House } from './house.ts';
```

以下是支持的形式:

<!-- skip -->
```typescript
import type T from './mod';
import type { A, B } from './mod';
import type * as Types from './mod';
export type { T };
export type { T } from './mod';
```
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ You can also download the Epub version here:
- [Optional Variance Annotations for Type Parameters](#optional-variance-annotations-for-type-parameters)
- [Symbol and Template String Pattern Index Signatures](#symbol-and-template-string-pattern-index-signatures)
- [The satisfies Operator](#the-satisfies-operator)
- [Type-Only Imports and Export](#type-only-imports-and-export)
<!-- markdownlint-enable MD004 -->
## Introduction

Expand Down Expand Up @@ -4723,3 +4724,29 @@ const user3 = {
user3.attributes?.map(console.log); // TypeScript infers correctly: string[]
user3.nickName; // TypeScript infers correctly: undefined
```

### Type-Only Imports and Export

Type-Only Imports and Export allows you to import or export types without importing or exporting the values or functions associated with those types. This can be useful for reducing the size of your bundle.

To use type-only imports, you can use the `import type keyword`.

TypeScript permits using both declaration and implementation file extensions (.ts, .mts, .cts, and .tsx) in type-only imports, regardless of `allowImportingTsExtensions` settings.

For example:

<!-- skip -->
```typescript
import type { House } from './house.ts';
```

The following are supported forms:

<!-- skip -->
```typescript
import type T from './mod';
import type { A, B } from './mod';
import type * as Types from './mod';
export type { T };
export type { T } from './mod';
```
Binary file modified downloads/typescript-book-zh_CN.epub
Binary file not shown.
Binary file modified downloads/typescript-book.epub
Binary file not shown.

0 comments on commit 795eb8f

Please sign in to comment.