过量属性检测

PR

看下官网的示例 (opens new window)

interface SquareConfig {
  color?: string;
  width?: number;
}

function createSquare (config: SquareConfig): { color: string; area: number } {
  return {
    color: config.color || 'red',
    area: config.width ? config.width * config.width : 20
  }
}

const config = { colour: 'red', width: 100 }

createSquare(config) // OK
createSquare({ colour: 'red', width: 100 }) // ERROR
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

Strict object literal assignment checking (opens new window) 应该是特地为对象字面量增加了严格模式。

疑惑

为什么仅为对象字面量增加严格模式呢,SF 中有一个探讨 (opens new window)

解释似乎很有道理,对象字面量仅在此处使用,应当严格限制,而引用的对象可能来自其他地方,如果你对其进行修改那么可能造成其余地方出现问题。

最后更新时间: 1 年前