[Typescript] 136. Medium - Object to Union
typescript to object union Medium 136
2023-09-14 08:59:11 时间
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;
}
>
>
];
相关文章
- [systemd]How To Use Systemctl to Manage Systemd Services and Units
- [Typescript] Identity function pattern - Using Identity function to enforce the type 01
- [Typescript] 102. Hard - String to Number
- [Typescript] 13. Easy - Unshift
- [Typescript Challenges] 6 Easy - Exclude
- [Typescript] Function Generics
- [TypeScript] Use the never type to avoid code with dead ends using TypeScript
- [TypeScript] Using Interfaces to Describe Types in TypeScript
- [Typescript] Write clean Type 2 - Reduce the usage in generic slot
- [Typescript] NoInfer
- [Typescript] Using Extract type until to get the value from Union type
- [Typescript] Force to valid the type by using Valid branded type
- [Typescript] Map a Discriminated Union to an Object
- [Typescript] 98. Medium - Append to object
- [Typescript] Tips: Derive a union type from an object
- [Typescript] Convert a js project to typescript
- [Typescript] Making TypeScript Stick - 3
- [TypeScript] Make typescript stick - 2
- [TypeScript] Class Property Inference from Constructors V4
- [React Typescript 2022] Use TypeScript to Type a React Class Component
- [React Typescript 2022] Type a Radio Button Component in React using TypeScript
- [Typescript] Narrow the unknown Type with TypeScript's Assertion Functions
- [Typescript] Extend Functionality of a TypeScript Class with Decorators
- [TypeScript] Shallow copy object by using spread opreator
- [Typescript] Sorting arrays in TypeScript
- [TypeScript] Use the never type to avoid code with dead ends using TypeScript
- [TypeScript] The Basics of Generics in TypeScript
- Object reference not set to an instance of an object.
- TypeScript里的中括号类型定义法
- 成功解决To fix this you could try to: 1. loosen the range of package versions you‘ve specified