- #110
TypeScript 验证类型是否完全相同的思考(Freshness 概念)
type User = {name: string;};function printUser(user: User) {console.log(user);}printUser({ name: 'Joe', email: 'joe@email.com' });// ❌ Error: Object literal may only specify known properties, and 'email' does not exist in type 'AllowedType'.const userData = {name: 'Joe',email: 'joe@email.com',};printUser(userData);// ✅ No errorTypeScript 遵循 Structural Type(结构类型系统),意味着如果两个类型有相同的结构,在这里的案例指「对象有相同的属性名称与类型」会被认为是相同的,即使是不同的类型名称,因此当传递一个变量给函数时,TypeScript 会根据结构来判断类型兼容性,只要变量包含函数所需的所有属性,就认为它们是兼容的。
但当直接传递一个对象字面量(Object Literal)给函数时,TypeScript 会进行更严格的检查,要求对象字面量完全符合函数参数的类型定义由于 Freshness 的概念,换句话说就是 Strict Object Literal Checking。
这种检查机制主要是为了提高代码的准确性和可靠性,避免拼写错误。
如果希望 TypeScript 进行严格的类型检查,这里是一个
Exact
的泛型变通方案可以采用:type Exact<A, B> = A extends B ? (B extends A ? A : never) : never;type User = {name: string;};function printUser<T>(user: Exact<T, User>) {console.log(user);}printUser({ name: 'Joe', email: 'joe@email.com' });// ❌ Error: Argument of type '{ name: string; email: string; }' is not assignable to parameter of type 'never'.const userData = {name: 'Joe',email: 'joe@email.com',};printUser(userData);// ❌ Error: Argument of type '{ name: string; email: string; }' is not assignable to parameter of type 'never'延伸阅读:
- #109
- #108
- #107
- #106
- #105
- #104
- #103
- #102
- #101
- #100
- #99
- #98
- #97
- #96
- #95
- #94
- #93
- #92
- #91
- #90
- #89
- #88
- #87
- #86
- #85
- #84
- #83
- #82
- #81
- #80
- #79
- #78
- #77
- #76
- #75
- #74
- #73
- #72
- #71
- #70
- #69
- #68
- #67
- #66
- #65
- #64
- #63
- #62
- #61
- #60
- #59
- #58
- #57
- #56
- #55
- #54
- #53
- #52
- #51
- #50
- #49
- #48
- #47
- #46
- #45
- #44
- #43
- #42
- #41
- #40
- #39
- #38
- #37
- #36
- #35
- #34
- #33
- #32
- #31
- #30
- #29
- #28
- #27
- #26
- #25
- #24
- #23
- #22
- #21
- #20
- #19
- #18
- #17
- #16
- #15
- #14
- #13
- #12
- #11
- #10
- #9
- #8
- #7
- #6
- #5
- #4
- #3
- #2
- #1