示例工程 04-object-types-structural

04-object-types-structural/02-excess-property-check/README.md

markdown · 0.9 KB

运行:04 · excess-property-check

点击「运行示例」后,左侧显示源码、右侧显示运行结果;console 输出会实时回传。

# 02 · excess-property-check
 
**演示**:新鲜对象字面量直接赋给 `User` 会检查多余属性;先存进变量再传入则只做结构匹配。
**运行**:在 `tutorials/typescript` 下执行 `npm run build`,再执行 `npm run serve`,打开
`http://localhost:8080/examples/04-object-types-structural/02-excess-property-check/`;开 Console。
**预期控制台输出**:
```text
1:Ada
2:Linus
```
**类型检查**:`node node_modules/typescript/bin/tsc -p examples/04-object-types-structural/02-excess-property-check/tsconfig.types.json`
**预期类型错误**(`demo/fresh-literal.ts`,由 `tsconfig.fail.json` 触发):
```text
demo/fresh-literal.ts(8,42): error TS2353: Object literal may only specify known properties, and 'email' does not exist in type 'User'.
```
**注意**:这是「新鲜度(freshness)」规则,不是结构化类型的例外。