不同范围变量的类型推断

时间:2019-01-06 09:29:02

标签: typescript tsc

考虑以下代码(在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>'; } 如何确定用于类型推断的变量有关。

1 个答案:

答案 0 :(得分:2)

这里有12种情况: (const, let, var) * (global, local) * (explicit string union, inferred string)

TL; DR

简单方法:

  • 如果您在外部范围中明确指定类型,则结果始终相同:"ok" | "no"的并集。

陷阱:

  • 因为内部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'
}