-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
各种 id 类型如:UserId, ProjectId, TaskId 等等,它们应该有下列性质: 1.可以赋给 string 类型(允许如:`const x: string = id as UserId`) 2.相互之间不可赋值(如遇 `const tid: TeamId = uid as UserId` 会报错, 需要手动强转) 3.string 不可以赋给它们(如遇 `const id: UserId = 'hello'` 会报错,需 要手动强转) 原来 `interface X extends String { kind?: 'X' }` 的实现,满足了2,但没 有满足1、3。 不满足1,导致当需要将 id 数据从带有场景上下文的业务代码传给不关心业务逻 辑而只是简单接受 string 的底层组件时,需要通过 `as string` 强转,如果 该信息包在一个对象结构里,那这个对象结构要么需要 `as any`,结果丢失所 有类型信息,要么底层组件的对应对象结构类型声明就需要添加类型参数(泛型 声明),结果增加冗长而意义不大的泛型声明。 而不满足3,会漏掉很多类型检查,因为并不是任何 string 类型的值都可以赋 值给特定 id 类型的。 新的写法是: `type X = string & { kind: 'X' }` 它能同时满足1、2、3。 参考: - https://codemix.com/opaque-types-in-javascript/ - microsoft/TypeScript#15807 - microsoft/TypeScript#4895 - microsoft/TypeScript#202 - https://github.com/Microsoft/TypeScript/blob/d9b93903c035e48c8da1d731332787f83efc4619/src/compiler/types.ts#L54
- Loading branch information
Showing
20 changed files
with
126 additions
and
125 deletions.
There are no files selected for viewing
This file contains 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 file contains 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 file contains 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 file contains 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 file contains 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 file contains 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 file contains 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 file contains 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 file contains 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 file contains 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 file contains 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 file contains 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 file contains 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
Oops, something went wrong.