考虑以下代码(在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
定义 用于确定返回类型。
使用var
时不会发生这种情况;此代码:
function test(): typeof status {
var status = 'hello'; // notice the var here
return 'successful';
}
产生预期的回报类型'successful' | 'failed'
。
使用let
:
function test(): typeof status {
let status = 'hello'; // notice the let here
return 'successful';
}
这会编译,但结果是返回类型为string
,将再次使用内部定义。
我希望tsc
使用status
定义的范围最高的test
来评估两种情况下的返回类型,而不管tsc
中存在什么声明。 为什么会观察到上述行为?这应该与function chance($input=array())
{
echo 'The Max Value can be: '.(array_sum($input)*10).'<br>';
$number=rand(0,array_sum($input)*10);
echo 'Checking for: '.$number.'<br>';
$starter=0;
foreach($input as $key => $val)
{
$starter+=$val*10;
echo 'Current value being tested against is: '.$starter.' which is '.$key.'<br>';
if($number<=$starter)
{
$ret=$key;
break;
}
}
return 'Winner is '.$ret.'';
}
$array=array('black' => 15.30, 'brown' => 20.20, 'kitty' => 15.30, 'lala' => 15.20, 'popi' => 14.00, 'usher' => 20.00);
for($i=0;$i<3;$i++)
{
echo chance($array).'<br><br>';
}
如何确定用于类型推断的变量有关。
答案 0 :(得分:2)
这里有12种情况:
(const, let, var) * (global, local) * (explicit string union, inferred string)
TL; DR
"ok" | "no"
的并集。let
和const
在返回位置可用于typeof
,所以它们遮盖了外部声明.. var
不能用于typeof
的返回类型位置(无论出于何种原因)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'
}