TypeScript泛型 – 与另一个对象具有相同的键,但所有类型均为’any’

huangapple go评论83阅读模式
英文:

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 };

Playground link

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>;

Playground link

英文:

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 };

Playground link

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>;

Playground link

答案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!

huangapple
  • 本文由 发表于 2023年5月21日 19:43:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76299743.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定