为什么我会收到错误"对象文字只能指定已知属性"?

时间:2015-08-04 17:55:36

标签: typescript

我刚从TypeScript 1.5升级到最新版本,并且我在代码中看到错误:

interface Options {
   /* ... others ... */
   callbackOnLocationHash?: boolean;
}

function f(opts: Options) { /* ... */ }

//  Error: Object literal may only specify known properties,
//     and 'callbackOnLoactionHash'does not exist in type 'Options'.
f( { callbackOnLoactionHash: false });

代码看起来很好。怎么了?

(替代宇宙版:我认识到错字,我真的很想写那个。我该怎么做才能删除错误?)

1 个答案:

答案 0 :(得分:101)

从TypeScript 1.6开始,对象文字中没有相应属性的属性会被标记为错误。

通常,此错误表示您的代码或定义文件中存在错误(通常是拼写错误)。在这种情况下正确的解决方法是修复错字。在问题中,属性callbackOnLoactionHash不正确,应该是callbackOnLocationHash(请注意错误拼写"位置")。

此更改还需要对定义文件进行一些更新,因此您应该为您正在使用的任何库获取最新版本的.d.ts。

示例:

interface TextOptions {
    alignment?: string;
    color?: string;
    padding?: number;
}
function drawText(opts: TextOptions) { ... }
drawText({ align: 'center' }); // Error, no property 'align' in 'TextOptions'

但我打算这样做

在某些情况下,您可能打算在对象中添加额外的属性。根据您正在做的事情,有几个适当的修复

仅检查某些属性的类型

有时候你想确保存在一些东西和正确的类型,但是无论出于什么原因都打算有额外的属性。类型断言(<T>vv as T)不检查额外属性,因此您可以使用它们代替类型注释:

interface Options {
    x?: string;
    y?: number;
}

// Error, no property 'z' in 'Options'
let q1: Options = { x: 'foo', y: 32, z: 100 };
// OK
let q2 = { x: 'foo', y: 32, z: 100 } as Options;
// Still an error (good):
let q3 = { x: 100, y: 32, z: 100 } as Options;

这些属性可能更多

某些API接受一个对象并动态迭代其键,但具有特殊的&#39;需要属于某种类型的密钥。向该类型添加字符串索引器将禁用额外的属性检查

<强>之前

interface Model {
  name: string;
}
function createModel(x: Model) { ... }

// Error
createModel({name: 'hello', length: 100});

<强>后

interface Model {
  name: string;
  [others: string]: any;
}
function createModel(x: Model) { ... }

// OK
createModel({name: 'hello', length: 100});

这是一只狗,一只猫或一匹马,还不确定

interface Animal { move; }
interface Dog extends Animal { woof; }
interface Cat extends Animal { meow; }
interface Horse extends Animal { neigh; }

let x: Animal;
if(...) {
  x = { move: 'doggy paddle', woof: 'bark' };
} else if(...) {
  x = { move: 'catwalk', meow: 'mrar' };
} else {
  x = { move: 'gallop', neigh: 'wilbur' };
}

这里有两个好的解决方案

x

指定一个封闭集
// Removes all errors
let x: Dog|Cat|Horse;

键入断言

// For each initialization
  x = { move: 'doggy paddle', woof: 'bark' } as Dog;

此类型有时是开放的,有时不是

&#34;数据模型的清洁解决方案&#34;使用交集类型的问题:

interface DataModelOptions {
  name?: string;
  id?: number;
}
interface UserProperties {
  [key: string]: any;
}
function createDataModel(model: DataModelOptions & UserProperties) {
 /* ... */
}
// findDataModel can only look up by name or id
function findDataModel(model: DataModelOptions) {
 /* ... */
}
// OK
createDataModel({name: 'my model', favoriteAnimal: 'cat' });
// Error, 'ID' is not correct (should be 'id')
findDataModel({ ID: 32 });

另见https://github.com/Microsoft/TypeScript/issues/3755