示例工程 12-variance-soundness
12-variance-soundness/03-method-bivariance/README.md
点击「运行示例」后,左侧显示源码、右侧显示运行结果;console 输出会实时回传。
# 03 · method-bivariance
**演示**:方法声明的参数是双变的,函数属性是逆变的;同一形状,两种声明兼容性不同。
**运行**:在 `tutorials/typescript` 下执行 `npm run build`,再执行 `npm run serve`,打开
`http://localhost:8080/examples/12-variance-soundness/03-method-bivariance/`;开 Console。
**预期控制台输出**:
```text
method:ok
```
**类型检查**:`node node_modules/typescript/bin/tsc -p examples/12-variance-soundness/03-method-bivariance/tsconfig.types.json`
**预期类型错误**(`demo/function-property.ts`,由 `tsconfig.fail.json` 触发):
```text
demo/function-property.ts(11,3): error TS2322: Type '(value: Dog) => void' is not assignable to type '(value: Animal) => void'.
Types of parameters 'value' and 'value' are incompatible.
```
**注意**:方法双变是为了兼容 `Array.prototype.push` 等既有 API,不是更安全的选择。