[Typescript] 136. Medium - Object to Union
typescript to object union Medium 136
2023-09-14 09:00:42 时间
import { Equal, Expect } from "../helpers/type-utils";
interface Attributes {
id: string;
email: string;
username: string;
}
/**
* How do we create a type helper that represents a union
* of all possible combinations of Attributes?
*/
type ObjectToUnion<T> = {
[Key in keyof T]: Record<Key, T[Key]>;
}[keyof T];
type ExclusiveAttributes = ObjectToUnion<Attributes>;
type tests = [
Expect<
Equal<
ExclusiveAttributes,
| {
id: string;
}
| {
email: string;
}
| {
username: string;
}
>
>
];
相关文章
- [Typescript] Identity function pattern - Using Identity function to enforce the type 01
- [Typescript] Map an Object to a Union of Tuples
- [Typescript] Map a Discriminated Union to an Object
- [Typescript] 85. Medium - IsInteger
- [Typescript] Get a TypeScript package ready for release to NPM
- [Typescript] 54. Medium - Tuple to Nested Object
- [TypeScript] Use the TypeScript "unknown" type to avoid runtime errors
- [TypeScript@2.5] Omit catch error block if not needed
- [Typescript] @typescript-eslint/unbound-method
- [Typescript] 137. Hard - Discriminated union to Object
- [Typescript] Map an Object to a Union of Tuples
- [Typescript] 114. Hard - Camelize
- [Typescript] 102. Hard - String to Number
- [Typescript] Prevent Type Widening of Object Literals with TypeScript's const Assertions
- [Typescript] Dynamic types: Use TypeScript's Mapped Types and Template Literal Types Together
- [Typescript] Narrow the unknown Type with TypeScript's Assertion Functions
- [TypeScript] Define Custom Type Guard Functions in TypeScript
- [TypeScript] Typescript Interfaces vs Aliases Union & Intersection Types
- [Typescript] Introduction to Generics in Typescript
- PAT 1033. To Fill or Not to Fill (贪心)
- cid unmatched [object Object] at view.umd.min.js:1 TypeError: Invalid attempt to destructure non-ite