我总是用noImplicitAny标记编译TypeScript。这是有意义的,因为我希望我的类型检查尽可能严格。

我的问题是,下面的代码我得到的错误:

Index signature of object type implicitly has an 'any' type
interface ISomeObject {
    firstKey:   string;
    secondKey:  string;
    thirdKey:   string;
}

let someObject: ISomeObject = {
    firstKey:   'firstValue',
    secondKey:  'secondValue',
    thirdKey:   'thirdValue'
};

let key: string = 'secondKey';

let secondValue: string = someObject[key];

需要注意的是,这里的思想是键变量来自应用程序中的其他地方,可以是对象中的任何键。

我已经尝试通过以下方式显式地转换类型:

let secondValue: string = <string>someObject[key];

或者我的场景只是不可能与-noImplicitAny?


当前回答

添加索引签名将让TypeScript知道应该是什么类型。

在你的例子中,这将是[key: string]: string;

interface ISomeObject {
    firstKey:      string;
    secondKey:     string;
    thirdKey:      string;
    [key: string]: string;
}

但是,这也强制所有属性类型与索引签名匹配。因为所有的属性都是一个字符串,所以它可以工作。

虽然索引签名是描述数组和“字典”模式的强大方法,但它们也强制所有属性匹配它们的返回类型。

编辑:

如果类型不匹配,则可以使用联合类型[key: string]: string|IOtherObject;

对于联合类型,最好让TypeScript推断类型,而不是定义类型。

// Type of `secondValue` is `string|IOtherObject`
let secondValue = someObject[key];
// Type of `foo` is `string`
let foo = secondValue + '';

尽管如果索引签名中有很多不同的类型,这可能会有点混乱。另一种方法是在签名中使用any。[key: string]: any;然后需要像上面那样强制转换类型。

其他回答

我有两个界面。第一个是别人的孩子。我做了以下几点:

父接口增加索引签名。 使用适当的类型使用as关键字。

完整代码如下:

子接口:

interface UVAmount {
  amount: number;
  price: number;
  quantity: number;
};

父接口:

interface UVItem  {
// This is index signature which compiler is complaining about.
// Here we are mentioning key will string and value will any of the types mentioned.
  [key: string]:  UVAmount | string | number | object;

  name: string;
  initial: UVAmount;
  rating: number;
  others: object;
};

反应组件:

let valueType = 'initial';

function getTotal(item: UVItem) {
// as keyword is the dealbreaker.
// If you don't use it, it will take string type by default and show errors.
  let itemValue = item[valueType] as UVAmount;

  return itemValue.price * itemValue.quantity;
}

添加索引签名将让TypeScript知道应该是什么类型。

在你的例子中,这将是[key: string]: string;

interface ISomeObject {
    firstKey:      string;
    secondKey:     string;
    thirdKey:      string;
    [key: string]: string;
}

但是,这也强制所有属性类型与索引签名匹配。因为所有的属性都是一个字符串,所以它可以工作。

虽然索引签名是描述数组和“字典”模式的强大方法,但它们也强制所有属性匹配它们的返回类型。

编辑:

如果类型不匹配,则可以使用联合类型[key: string]: string|IOtherObject;

对于联合类型,最好让TypeScript推断类型,而不是定义类型。

// Type of `secondValue` is `string|IOtherObject`
let secondValue = someObject[key];
// Type of `foo` is `string`
let foo = secondValue + '';

尽管如果索引签名中有很多不同的类型,这可能会有点混乱。另一种方法是在签名中使用any。[key: string]: any;然后需要像上面那样强制转换类型。

创建一个接口来定义'indexer'接口

然后用该索引创建对象。

注意:这仍然会有其他答案所描述的关于强制每个项目的类型的相同问题-但这通常正是你想要的。

您可以根据需要设置泛型类型参数:ObjectIndexer< Dog | Cat>

// this should be global somewhere, or you may already be 
// using a library that provides such a type
export interface ObjectIndexer<T> {
  [id: string]: T;
}

interface ISomeObject extends ObjectIndexer<string>
{
    firstKey:   string;
    secondKey:  string;
    thirdKey:   string;
}

let someObject: ISomeObject = {
    firstKey:   'firstValue',
    secondKey:  'secondValue',
    thirdKey:   'thirdValue'
};

let key: string = 'secondKey';

let secondValue: string = someObject[key];

打印稿操场


你甚至可以在定义泛型类型时在泛型约束中使用this:

导出类SmartFormGroup<T extends IndexableObject<any>> extends FormGroup

然后类中的T可以被索引:-)

另一种避免错误的方法是像这样使用强制转换:

let secondValue: string =(<任意>someObject)[key]; (注意括号)

唯一的问题是,这不再是类型安全的,因为您正在强制转换为任何。但是您总是可以转换回正确的类型。

ps:我使用的是typescript 1.7,不确定以前的版本。

使用typeof键

const cat = {
    name: 'tuntun'
}

const key: string = 'name' 

cat[key as keyof typeof cat]