英文:
TypeScript Generic - Same keys as another object, but all types as 'any'
问题
给定一个类 `Foo`
class Foo {
foo: string;
bar: number;
baz: Date;
}
我想要创建一个名为 `Transmuted<T>` 的通用类型,它会返回这个接口:
interface TransmutedFoo {
foo: any
bar: any
baz: any
}
我该如何创建这个通用类型?
英文:
Given a class Foo
class Foo {
foo: string;
bar: number;
baz: Date;
}
I want to create a generic type called Transmuted<T>
that would return this interface:
interface TransmutedFoo {
foo: any
bar: any
baz: any
}
How can I create this generic?
答案1
得分: 1
When you use class Foo {/*...*/}
, TypeScript creates a type called Foo
to represent instances of the class. So fundamentally, your question comes down to: How do I convert one object type to another type with the same keys?
The answer is a mapped type:
type Transmuted<T> = {
[Key in keyof T]: any;
};
[Key in keyof T]
lists the property keys of T
, and what appears after the :
is the type for those properties. In this case, it's a very simple mapped type since it maps everything to any
.
Applying that to Foo
gives us:
class Foo {
foo: string;
bar: number;
baz: Date;
constructor() {
this.foo = "";
this.bar = 0;
this.baz = new Date();
}
}
type Transmuted<T> = {
[Key in keyof T]: any;
};
type TransmutedFoo = Transmuted<Foo>;
// ^? type TransmutedFoo = { foo: any; bar: any; baz: any };
In this specific case, since you want all of the properties to have the same type, you could use the built-in generic type Record<Keys, Type>
as Aluan Haddad points out rather than writing it explicitly as above. Record
"Constructs an object type whose property keys are Keys and whose property values are Type." (And it's implemented by doing exactly what we do above: { [P in Keys]: Type; }
.) So you could do:
type Transmuted<T> = Record<keyof T, any>;
英文:
When you use class Foo {/*...*/}
, TypeScript creates a type called Foo
to represent instances of the class. So fundamentally, your question comes down to: How do I convert one object type to another type with the same keys?
The answer is a mapped type:
type Transmuted<T> = {
[Key in keyof T]: any;
};
[Key in keyof T]
lists the property keys of T
, and what appears after the :
is the type for those properties. In this case, it's a very simple mapped type since it maps everything to any
.
Applying that to Foo
gives us:
class Foo {
foo: string;
bar: number;
baz: Date;
constructor() {
this.foo = "";
this.bar = 0;
this.baz = new Date();
}
}
type Transmuted<T> = {
[Key in keyof T]: any;
};
type TransmutedFoo = Transmuted<Foo>;
// ^? type TransmutedFoo = { foo: any; bar: any; baz: any };
In this specific case, since you want all of the properties to have the same type, you could use the built-in generic type Record<Keys, Type>
as Aluan Haddad points out rather than writing it explicitly as above. Record
"Constructs an object type whose property keys are Keys and whose property values are Type." (And it's implemented by doing exactly what we do above: { [P in Keys]: Type; }
.) So you could do:
type Transmuted<T> = Record<keyof T, any>;
答案2
得分: 1
你可以通过创建一个通用类型来实现这一点,这种情况下我们称之为 Transmuted<T>
。这个通用类型将把 T
的属性转换为 any
类型。请看下面的代码示例:
class Foo {
foo: string;
bar: number;
baz: Date;
}
type Transmuted<T> = {
[K in keyof T]: any;
}
type TransmutedFoo = Transmuted<Foo>
因此,我们使用了映射类型来遍历 T
的每个属性 K
,使用 TypeScript 的 keyof
运算符,然后将 any
类型分配给每个属性。希望这有所帮助!
英文:
You can achieve this by creating a generic type, in this case we call it Transmuted<T>
. This generic type will transform the properties of T
into any
type. See below for code on how to go about it:
class Foo {
foo: string;
bar: number;
baz: Date;
}
type Transmuted<T> = {
[K in keyof T]: any;
}
type TransmutedFoo = Transmuted<Foo>
So we use a mapped type to iterate through each of the properties K
of T
using the TypeScript keyof
operator, and then, type any
is assigned as the type for each of the property. Hope this helps!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论