不同作用域变量的类型推断
Type inference of differently scoped variables
考虑以下代码(TypeScript 3.2.2 中的运行):
const status: 'successful' | 'failed' = 'successful';
function test(): typeof status {
const status = 'hello';
return 'successful';
}
这不会编译,因为 test
的 return 类型与其签名不匹配:
error TS2322: Type '"successful"' is not assignable to type 'IResult<"hello">'.
出于某种原因,status
定义 在 函数中用于确定 return 类型。
使用 var
时不会发生这种情况;此代码:
function test(): typeof status {
var status = 'hello'; // notice the var here
return 'successful';
}
生成预期的 return 类型 'successful' | 'failed'
。
使用let
:
function test(): typeof status {
let status = 'hello'; // notice the let here
return 'successful';
}
这会编译,但结果是 return 类型是 string
,再次使用内部定义。
我希望 tsc
使用范围中定义最高的 status
在这两种情况下评估其 return 类型,而不管 [=14 中存在什么声明=]. 为什么会出现上述行为? 这应该与 tsc
如何决定使用哪些变量进行类型推断有关。
这里有12个案例:
(const, let, var) * (global, local) * (explicit string union, inferred string)
TL;DR
简单方法:
- 如果您在外部范围内显式指定类型 - 结果始终相同:union of
"ok" | "no"
.
陷阱:
- 因为内部
let
和 const
可用于 typeof
在 return 位置,它们隐藏外部声明 ..
- 内部
var
不适用于 typeof
在 return 类型位置(无论出于何种原因)
添加额外的混淆:
- 如果您尝试推断:
let
和 var
将认为类型是 string
,而 const
将类型精确到 [不可变的原始] 值。
参考案例列表:
// Infer, Inner
function test_inner_let(): typeof status01 { // type is string
let status01 = 'ok'; // let is mutable, so type is only string
return 'lol';
}
function test_inner_const(): typeof status02 { // type is 'ok'
const status02 = 'ok'; // const allows to specify type to exact 'ok'
return 'lol'; // error, 'lol' is not assignable to 'ok'
}
function test_inner_var(): typeof status03 { // type is any, TS warning: status03 not found
var status03 = 'ok'; // var is mutable, so type is string
return 'lol';
}
// Explicit, Inner
function test_inner_let_t(): typeof status11 { // type is union 'ok'|'no'
let status11: 'ok' | 'no' = 'ok';
return 'lol'; // error, 'lol' is not assignable to 'ok'|'no'
}
function test_inner_const_t(): typeof status12 { // type is union 'ok'|'no'
const status12: 'ok' | 'no' = 'ok';
return 'lol'; // error, 'lol' is not assignable to 'ok'|'no'
}
function test_inner_var_t(): typeof status13 { // type is any, TS warning: status13 not found
var status13: 'ok' | 'no' = 'ok';
return 'lol';
}
// Explicit, Outer - everything works the same
let status21: 'ok' | 'no' = 'ok';
function test_outer_let_t(): typeof status21 { // type is union 'ok'|'no'
return 'lol'; // error, 'lol' is not assignable to 'ok'|'no'
}
const status22: 'ok' | 'no' = 'ok';
function test_outer_const_t(): typeof status22 { // type is union 'ok'|'no'
return 'lol'; // error, 'lol' is not assignable to 'ok'|'no'
}
var status23: 'ok' | 'no' = 'ok';
function test_outer_var_t(): typeof status23 { // type is union 'ok'|'no'
return 'lol'; // error, 'lol' is not assignable to 'ok'|'no'
}
// Infer, Outer
let status31 = 'ok'; // type is string
function test_outer_let(): typeof status31 { // type is string
return 'lol';
}
const status32 = 'ok'; // const allows to specify type to exact 'ok'
function test_outer_const(): typeof status32 { // type is 'ok'
return 'lol'; // error, 'lol' is not assignable to 'ok'
}
var status33 = 'ok'; // var is mutable, so type is string
function test_outer_var(): typeof status33 { // type is string
return 'lol';
}
// (Explicit, Outer const) + (Implicit, Inner)
const status41: 'ok' | 'no' = 'ok';
function test_combo_let(): typeof status41 { // type is string, inner let took preference
let status41 = 'ok';
return 'lol';
}
const status42: 'ok' | 'no' = 'ok';
function test_combo_const(): typeof status42 { // type is 'sorry', inner const took preference
const status42 = 'sorry';
return 'lol'; // error, 'lol' is not assignable to 'sorry'
}
const status43: 'ok' | 'no' = 'ok';
function test_combo_var(): typeof status43 { // type is union 'ok'|'no', var is not bubling up
var status = 'whatever';
return 'lol'; // error, 'lol' is not assignable to 'ok'|'no'
}
考虑以下代码(TypeScript 3.2.2 中的运行):
const status: 'successful' | 'failed' = 'successful';
function test(): typeof status {
const status = 'hello';
return 'successful';
}
这不会编译,因为 test
的 return 类型与其签名不匹配:
error TS2322: Type '"successful"' is not assignable to type 'IResult<"hello">'.
出于某种原因,status
定义 在 函数中用于确定 return 类型。
使用 var
时不会发生这种情况;此代码:
function test(): typeof status {
var status = 'hello'; // notice the var here
return 'successful';
}
生成预期的 return 类型 'successful' | 'failed'
。
使用let
:
function test(): typeof status {
let status = 'hello'; // notice the let here
return 'successful';
}
这会编译,但结果是 return 类型是 string
,再次使用内部定义。
我希望 tsc
使用范围中定义最高的 status
在这两种情况下评估其 return 类型,而不管 [=14 中存在什么声明=]. 为什么会出现上述行为? 这应该与 tsc
如何决定使用哪些变量进行类型推断有关。
这里有12个案例:
(const, let, var) * (global, local) * (explicit string union, inferred string)
TL;DR
简单方法:
- 如果您在外部范围内显式指定类型 - 结果始终相同:union of
"ok" | "no"
.
陷阱:
- 因为内部
let
和const
可用于typeof
在 return 位置,它们隐藏外部声明 .. - 内部
var
不适用于typeof
在 return 类型位置(无论出于何种原因)
添加额外的混淆:
- 如果您尝试推断:
let
和var
将认为类型是string
,而const
将类型精确到 [不可变的原始] 值。
参考案例列表:
// Infer, Inner
function test_inner_let(): typeof status01 { // type is string
let status01 = 'ok'; // let is mutable, so type is only string
return 'lol';
}
function test_inner_const(): typeof status02 { // type is 'ok'
const status02 = 'ok'; // const allows to specify type to exact 'ok'
return 'lol'; // error, 'lol' is not assignable to 'ok'
}
function test_inner_var(): typeof status03 { // type is any, TS warning: status03 not found
var status03 = 'ok'; // var is mutable, so type is string
return 'lol';
}
// Explicit, Inner
function test_inner_let_t(): typeof status11 { // type is union 'ok'|'no'
let status11: 'ok' | 'no' = 'ok';
return 'lol'; // error, 'lol' is not assignable to 'ok'|'no'
}
function test_inner_const_t(): typeof status12 { // type is union 'ok'|'no'
const status12: 'ok' | 'no' = 'ok';
return 'lol'; // error, 'lol' is not assignable to 'ok'|'no'
}
function test_inner_var_t(): typeof status13 { // type is any, TS warning: status13 not found
var status13: 'ok' | 'no' = 'ok';
return 'lol';
}
// Explicit, Outer - everything works the same
let status21: 'ok' | 'no' = 'ok';
function test_outer_let_t(): typeof status21 { // type is union 'ok'|'no'
return 'lol'; // error, 'lol' is not assignable to 'ok'|'no'
}
const status22: 'ok' | 'no' = 'ok';
function test_outer_const_t(): typeof status22 { // type is union 'ok'|'no'
return 'lol'; // error, 'lol' is not assignable to 'ok'|'no'
}
var status23: 'ok' | 'no' = 'ok';
function test_outer_var_t(): typeof status23 { // type is union 'ok'|'no'
return 'lol'; // error, 'lol' is not assignable to 'ok'|'no'
}
// Infer, Outer
let status31 = 'ok'; // type is string
function test_outer_let(): typeof status31 { // type is string
return 'lol';
}
const status32 = 'ok'; // const allows to specify type to exact 'ok'
function test_outer_const(): typeof status32 { // type is 'ok'
return 'lol'; // error, 'lol' is not assignable to 'ok'
}
var status33 = 'ok'; // var is mutable, so type is string
function test_outer_var(): typeof status33 { // type is string
return 'lol';
}
// (Explicit, Outer const) + (Implicit, Inner)
const status41: 'ok' | 'no' = 'ok';
function test_combo_let(): typeof status41 { // type is string, inner let took preference
let status41 = 'ok';
return 'lol';
}
const status42: 'ok' | 'no' = 'ok';
function test_combo_const(): typeof status42 { // type is 'sorry', inner const took preference
const status42 = 'sorry';
return 'lol'; // error, 'lol' is not assignable to 'sorry'
}
const status43: 'ok' | 'no' = 'ok';
function test_combo_var(): typeof status43 { // type is union 'ok'|'no', var is not bubling up
var status = 'whatever';
return 'lol'; // error, 'lol' is not assignable to 'ok'|'no'
}