打字稿中的记录类型是什么? [英] What is the Record type in typescript?
问题描述
Record
在 Typescript 中是什么意思?
Typescript 2.1 引入了 Record
类型,举例说明:
//对于T类型的每个属性K,将其转换为Ufunction mapObject(obj: Record, f: (x: T) => U): Record
见 打字稿 2.1
Advanced Types 页面提到了 Recordcode> 位于
Readonly
、Partial
和 Pick
旁边的 Mapped Types 标题下,其定义如下:
type Record= {[K中的P]:T;}
<块引用>
Readonly、Partial 和 Pick 是同态的,而 Record 不是.Record 不是同态的一个线索是它不需要输入类型来复制属性:
type ThreeStringProps = Record<'prop1' |'prop2' |'prop3', 字符串>
就是这样.除了以上引用之外,在 typescriptlang.org.
问题
谁能给
Record
一个简单的定义?Record<K,T>
仅仅是一种表达此对象上的所有属性都具有T
类型"的方式吗?可能不是所有属性,因为K
有一些目的......K
泛型是否禁止在对象上添加不是K
的键,或者它是否允许它们并且只是表明它们的属性没有转换为T
?以给定的例子:
type ThreeStringProps = Record<'prop1' |'prop2' |'prop3', 字符串>
和这个完全一样吗?:
type ThreeStringProps = {prop1: string, prop2: string, prop3: string}
- 谁能给
Record
一个简单的定义?
A Record
是一种对象类型,其属性键为 K
,属性值为 T
.也就是说,keyof Record
等价于 K
,而 Record
是(基本上)相当于T
.
Record<K,T>
是否只是一种表示此对象上的所有属性都具有T
类型"的方式?可能不是所有的对象,因为K
有一些目的......
正如您所注意到的,K
有一个目的……将属性键限制为特定值.如果您想接受所有可能的字符串值键,您可以执行类似 Record
的操作,但惯用的方法是使用 索引签名 像 { [k: string]: T }
.
K
泛型是否禁止在对象上添加不是K
的键,或者它是否允许它们并且只是表明它们的属性没有转换为?
它并没有完全禁止"额外的键:毕竟,通常允许一个值具有在其类型中没有明确提及的属性……但它不会识别出这样的属性存在:
declare const x: Record<"a", string>;x.b;//错误,属性 'b' 在类型 'Record<"a", string>' 上不存在
它会将它们视为多余的属性有时会被拒绝:
declare function acceptR(x: Record<"a", string>): void;acceptR({a: "嘿", b: "你"});//错误,对象字面量只能指定已知属性
有时被接受:
const y = {a: "嘿", b: "你"};接受R(y);//好的
<块引用>
以给定的例子:
type ThreeStringProps = Record<'prop1' |'prop2' |'prop3', 字符串>
和这个完全一样吗?:
type ThreeStringProps = {prop1: string, prop2: string, prop3: string}
是的!
希望有所帮助.祝你好运!
What does Record<K, T>
mean in Typescript?
Typescript 2.1 introduced the Record
type, describing it in an example:
// For every properties K of type T, transform it to U function mapObject<K extends string, T, U>(obj: Record<K, T>, f: (x: T) => U): Record<K, U>
see Typescript 2.1
And the Advanced Types page mentions Record
under the Mapped Types heading alongside Readonly
, Partial
, and Pick
, in what appears to be its definition:
type Record<K extends string, T> = { [P in K]: T; }
Readonly, Partial and Pick are homomorphic whereas Record is not. One clue that Record is not homomorphic is that it doesn’t take an input type to copy properties from:
type ThreeStringProps = Record<'prop1' | 'prop2' | 'prop3', string>
And that's it. Besides the above quotes, there is no other mention of Record
on typescriptlang.org.
Questions
Can someone give a simple definition of what
Record
is?Is
Record<K,T>
merely a way of saying "all properties on this object will have typeT
"? Probably not all properties, sinceK
has some purpose...Does the
K
generic forbid additional keys on the object that are notK
, or does it allow them and just indicate that their properties are not transformed toT
?With the given example:
type ThreeStringProps = Record<'prop1' | 'prop2' | 'prop3', string>
Is it exactly the same as this?:
type ThreeStringProps = {prop1: string, prop2: string, prop3: string}
- Can someone give a simple definition of what
Record
is?
A Record<K, T>
is an object type whose property keys are K
and whose property values are T
. That is, keyof Record<K, T>
is equivalent to K
, and Record<K, T>[K]
is (basically) equivalent to T
.
- Is
Record<K,T>
merely a way of saying "all properties on this object will have typeT
"? Probably not all objects, sinceK
has some purpose...
As you note, K
has a purpose... to limit the property keys to particular values. If you want to accept all possible string-valued keys, you could do something like Record<string, T>
, but the idiomatic way of doing that is to use an index signature like { [k: string]: T }
.
- Does the
K
generic forbid additional keys on the object that are notK
, or does it allow them and just indicate that their properties are not transformed toT
?
It doesn't exactly "forbid" additional keys: after all, a value is generally allowed to have properties not explicitly mentioned in its type... but it wouldn't recognize that such properties exist:
declare const x: Record<"a", string>;
x.b; // error, Property 'b' does not exist on type 'Record<"a", string>'
and it would treat them as excess properties which are sometimes rejected:
declare function acceptR(x: Record<"a", string>): void;
acceptR({a: "hey", b: "you"}); // error, Object literal may only specify known properties
and sometimes accepted:
const y = {a: "hey", b: "you"};
acceptR(y); // okay
With the given example:
type ThreeStringProps = Record<'prop1' | 'prop2' | 'prop3', string>
Is it exactly the same as this?:
type ThreeStringProps = {prop1: string, prop2: string, prop3: string}
Yes!
Hope that helps. Good luck!
这篇关于打字稿中的记录类型是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!