[Typescript] 61. Medium. - Flip
typescript Medium 61 Flip
2023-09-14 09:00:44 时间
Implement the type of just-flip-object
. Examples:
Flip<{ a: "x", b: "y", c: "z" }>; // {x: 'a', y: 'b', z: 'c'}
Flip<{ a: 1, b: 2, c: 3 }>; // {1: 'a', 2: 'b', 3: 'c'}
Flip<{ a: false, b: true }>; // {false: 'a', true: 'b'}
No need to support nested objects and values which cannot be object keys such as arrays
/* _____________ Your Code Here _____________ */
type Flip<T extends Record<PropertyKey, string | number| boolean>> = {
[Key in keyof T as `${T[Key]}`]: Key
}
/* _____________ Test Cases _____________ */
import type { Equal, Expect, NotEqual } from '@type-challenges/utils'
type cases = [
Expect<Equal<{ a: 'pi' }, Flip<{ pi: 'a' }>>>,
Expect<NotEqual<{ b: 'pi' }, Flip<{ pi: 'a' }>>>,
Expect<Equal<{ 3.14: 'pi'; true: 'bool' }, Flip<{ pi: 3.14; bool: true }>>>,
Expect<Equal<{ val2: 'prop2'; val: 'prop' }, Flip<{ prop: 'val'; prop2: 'val2' }>>>,
]
相关文章
- [Typescript] 86. Medium - ToPrimitive
- [Typescript] 80. Medium - Construct Tuple
- [Typescript] 79. Medium - MapTypes
- [Typescript] 51. Medium - Mutable
- [Typescript Challenges] 5. Easy - Length of Tuple
- [Typescript Challenges] 3. Easy - Tuple to Object
- [Typescript] Emitting Declaration Files
- [Typescript] Typing “this” and “noImplicitThis”
- [Parcel] Running TypeScript with parcel-bundler
- [Algorithms] Quicksort algorithm using TypeScript
- [TypeScript] Typescript Interfaces vs Aliases Union & Intersection Types
- [Typescript] 87. Medium - DeepMutable
- [Typescript] 75. Easy - Push
- [Typescript] 64. Hard - AllCombinations
- [Typescript] 36. Medium - Merge
- [Typescript Challenges] 28. Medium - Replace
- [Typescript] Typing “this” and “noImplicitThis”
- [Vuex] Split Vuex Store into Modules using TypeScript
- [TypeScript] Collect Related Strings in a String Enum in TypeScript
- [TypeScript] Type check JavaScript files using JSDoc and Typescript 2.5
- [TypeScript] Inheritance
- [Angular 2] Use Service use Typescript
- TypeScript Downleveling - 什么是 TypeScript 的降级行为
- TypeScript里的工具类型Partial的用法