0

方法调用

  this.onChangeScoreGradeType(this.gradingKeyForm.get("scoreGradeType").value.key);

方法定义

onChangeScoreGradeType(scoreGradeType: KeyValue<string,string>) 
{

}

this.gradingKeyForm.get("scoreGradeType").value

是类型KeyValue<string,string>

为什么会出现没有编译错误,那

this.gradingKeyForm.get("scoreGradeType").value.key

哪个字符串不适合KeyValue<string,string>实例?

我必须在我的 TypeScript 设置中进行哪些更改?

export default class KeyValue<TKey,TValue>
{
    constructor(public key: TKey,public value: TValue)
    {

    }
}

tsconfig.json

{
  "compilerOptions": {
    "baseUrl": "",
    "declaration": false,
    "emitDecoratorMetadata": true,
    "experimentalDecorators": true,
    "lib": ["es6", "dom"],
    "mapRoot": "./",
    "module": "es6",
    "moduleResolution": "node",
    "outDir": "../dist/out-tsc",
    "sourceMap": true,
    "target": "es5",
    "typeRoots": [
      "../node_modules/@types"
    ]
  }
}
4

1 回答 1

0

this.gradingKeyForm.get("scoreGradeType").value.key类型是TKeyany如果无法推断泛型,则泛型变为。因此key可能是any使其与所有东西兼容的类型。

于 2017-04-24T00:57:09.023 回答