简体   繁体   English

在打字稿中使用数字的Object.keys

[英]Object.keys using numbers in typescript

type Foo = { [key: number]: string }

const foo: Foo = { 100: 'foo', 200: 'bar' }
const sizes: number[] = Object.keys(foo)

Gives me: 给我:

Type 'string[]' is not assignable to type 'number[]

Why does Typescript assume string[] ? 为什么Typescript假定为string[] And what is the correct way of creating an array of numbers here? 在这里创建数字数组的正确方法是什么?

Object properties names are always string, if you want to convert property names to numbers on the fly, you can use map(Number) : 对象属性名称始终是字符串,如果您想将属性名称即时转换为数字,则可以使用map(Number)

const sizes: number[] = Object.keys(foo).map(Number);

Pay attention because, if your property name can't be converter to number, you'll get NaN in your sizes array 请注意,因为如果您的属性名称不能转换为数字,则尺寸数组中将显示NaN

All keys are strings in JavaScript - just use map : 所有键都是JavaScript中的字符串-只需使用map

const sizes: number[] = Object.keys(foo).map(Number);

This'll only work for numeric strings - if it's a European string involving decimals, for instance, it'll be NaN , and that never ends well. 这仅适用于数字字符串-例如,如果它是包含小数的欧洲字符串,则将是NaN ,并且永远都不会结束。

 console.log(Number("10,7")); 

Or change either of your types: 或更改您的两种类型:

const sizes: string[] = Object.keys(foo);

Or: 要么:

type Foo = { [key: string]: string };

Object.keys always returns an array of strings : Object.keys总是返回一个字符串数组:

 const obj = { prop: 'prop' }; console.log(typeof Object.keys(obj)[0]); 

Keys are always strings, even numeric keys like with arrays and with your Foo object - if you use a numeric key rather than a string key, it'll be coerced to a string automatically. 键始终是字符串,甚至是数组和Foo对象之类的数字键-如果您使用数字键而不是字符串键,它将自动强制为字符串。

I'd use [key: string] instead, because that's what the type of the keys are internally. 我会改用[key: string] ,因为那是内部键的类型。

Javascript Object has no number keys! Javascript对象没有数字键! All keys are Strings. 所有键都是字符串。 Always.If you want to map other things to values you should use a map . 始终。如果要将其他内容映射到值,则应使用map

 var numObj = { 1:'one', 2:'two' }; var numObjKey = Object.keys(numObj); console.log(typeof numObjKey[0]); console.log(typeof numObjKey.map(Number)[0]); 

Why does Typescript assume string[]? 为什么Typescript假定为string []? And what is the correct way of creating an array of numbers here? 在这里创建数字数组的正确方法是什么?

Because object property names are always strings (or, to properly explain that, are always coerced to strings), hence the default signature assumes a string[] as default type, as the intellisense even suggests: 由于对象属性名称始终是字符串(或者为了正确解释,总是被强制转换为字符串),因此默认签名将string[]假定为默认类型,如智能感知甚至建议的那样:

在此处输入图片说明

Besides, you can't ensure that, at runtime, those keys will be numbers . 此外, 您不能确保在运行时这些键将是number Defining a type that strictly defines that object to have numeric keys has no effect at runtime , hence it's always safer to safely cast the values to the desired type: 定义严格定义该对象以使其具有数字键的类型在运行时无效 ,因此,将值安全地强制转换为所需类型总是更安全的:

const sizes: number[] = Object.keys(foo).map(Number); // beware of possible NaN here.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM