[Typescript] 120. Hard - ObjectFromEntries
typescript Hard 120
2023-09-14 08:59:11 时间
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 Challenges] 144 Hard - Transpose
- [Typescript] Using Extract type until to get the value from Union type
- [Typescript] Declaring Globals for a Library
- [Typescript 4.9] TypeScript 4.9: satisfies operator
- [Typescript] 124. Binary to Decimal
- [Typescript] 112. Hard - DeepPick
- [Typescript] 111. Hard - String Join
- [Typescript] 104. Hard - Tuple to Enum Object
- [Typescript] 84. Medium - GetMiddleElement
- [Typescript] 18. Medium - Deep Readonly
- [Typescript] Use the Optional Chaining Operator in TypeScript
- [Vuex] Create a Vuex Store using TypeScript
- [Algorithm] Median Maintenance algorithm implementation using TypeScript / JavaScript
- [TypeScript] Generic Functions, class, Type Inference and Generics
- [Typescript] Generics in Type Arguments
- [Typescript] @typescript-eslint/unbound-method
- [Typescript] 123. Hard - Intersection
- [Typescript] 120. Hard - ObjectFromEntries
- [Typescript] 93. Hard - Get Required
- [Typescript] 42. Medium - Remove Index Signature
- [Typescript] Index access types
- [React Typescript 2022] Use TypeScript to Type a React Class Component
- [Typescript] Generics using TypeScript
- [TypeScript] Function Overloads in Typescript
- TypeScript Mixins 概念介绍