KScript 文档
ZH

错误代码

KS5020

在 strict 文件中,检查器无法证明其类型的值。在声明该值的地方写出类型。在 strict 文件中这是错误,脚本在修复之前不会运行:

<strict />

<let name="names" value="[]" /> <!-- error: write the item type, such as $names: string[] = [] -->
<let name="found" value="null" /> <!-- error: write its type: $found: string? = null -->
<let name="data" type="any" value="'{&quot;n&quot;: 1}'->convert(from: json)" />
<let name="n" value="$data.n" /> <!-- error: the checker can't tell what $n holds; write $n: int = $data.n -->
strict

$names = []                   // error: write the item type, such as $names: string[] = []
$found = null                 // error: write its type: $found: string? = null
$data: any = '{"n": 1}'->convert(from: json)
$n = $data.n                  // error: the checker can't tell what $n holds; write $n: int = $data.n

同一代码还会报告:被赋予不同类型值的变量、无从推断类型的 lambda 参数(写 fn(n: number) => ...)、结果无法推断的函数(写 fn first(items: list): string),以及既无类型也无默认值的字段。