gpt4 book ai didi

typescript - 不同作用域变量的类型推断

转载 作者:搜寻专家 更新时间:2023-10-30 20:59:38 26 4
gpt4 key购买 nike

考虑以下代码(在 TypeScript 3.2.2 中运行):

const status: 'successful' | 'failed' = 'successful';

function test(): typeof status {
const status = 'hello';

return 'successful';
}

这不会编译,因为 test 的返回类型和它的签名不匹配:

error TS2322: Type '"successful"' is not assignable to type 'IResult<"hello">'.

出于某种原因,status 定义 inside 函数被用于确定返回类型。

使用 var 时不会发生这种情况;这段代码:

function test(): typeof status {
var status = 'hello'; // notice the var here

return 'successful';
}

产生'成功'的预期返回类型 | “失败”

使用 let:

function test(): typeof status {
let status = 'hello'; // notice the let here

return 'successful';
}

此编译但返回类型为 string 的效果,再次使用内部定义。

我希望 tsc 使用范围中定义最高的 status 来评估两种情况下的返回类型,而不管 中存在什么声明测试为什么会出现上述行为?这应该与 tsc 如何决定使用哪些变量进行类型推断有关。

最佳答案

这里有12个案例:(const, let, var) * (global, local) * (显式字符串联合,推断字符串)

长话短说

简单方法:

  • 如果您在外部范围内显式指定类型 - 结果始终相同:union of "ok"| “不”

陷阱:

  • 因为内部 letconst 在返回位置可用于 typeof,所以它们隐藏外部声明..
  • 内部 var 在返回类型位置(无论出于何种原因)对于 typeof 不可用

添加额外的混淆:

  • 如果您尝试推断:letvar 将认为类型是 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 - 不同作用域变量的类型推断,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54060229/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com