Implement the type version of Object.fromEntries
For example:
interface Model {
name: string;
age: number;
locations: string[] | null;
}
type ModelEntries = ['name', string] | ['age', number] | ['locations', string[] | null];
type result = ObjectFromEntries<ModelEntries> // expected to be Model
/* _____________ Your Code Here _____________ */
type ObjectFromEntries<T extends any[]> = {
[Key in T as Key extends any[] ? Key[0] extends string ? Key[0]: never: never]: Key[1]
}
/* _____________ Test Cases _____________ */
import type { Equal, Expect } from '@type-challenges/utils'
interface Model {
name: string
age: number
locations: string[] | null
}
type ModelEntries = ['name', string] | ['age', number] | ['locations', string[] | null]
type cases = [
Expect<Equal<ObjectFromEntries<ModelEntries>, Model>>,
]
标签:Typescript,string,_____________,number,120,ObjectFromEntries,Key,null,type From: https://www.cnblogs.com/Answer1215/p/16932429.html