Comments (7)
interface Person {
name: string;
age?: number;
gender?: number;
income?: number;
}
// 只能包含Keys中唯一的一个Key
type RequireExactlyOne<T, Keys extends keyof T, K extends keyof T = Keys> = Keys extends any
? Omit<T, K> & Required<Pick<T, Keys>> & Partial<Record<Exclude<K, Keys>, never>>
: never;
type T1 = RequireExactlyOne<Person, "age" | "gender" | "income">;
const p1: T1 = {
name: "lolo",
age: 7,
};
const p2: T1 = {
name: "lolo",
gender: 1
};
const p3: T1 = {
name: "lolo",
income: 100
};
// Error
const p4: T1 = {
name: "lolo",
age: 7,
gender: 1,
};
const p5: T1 = {
name: "lolo",
age: 7,
income: 1,
};
const p6: T1 = {
name: "lolo",
gender: 7,
income: 1,
};
const p7: T1 = {
name: "lolo",
age: 8,
gender: 7,
income: 1,
};
解题思路: 利用联合类型 extends 实现分布执行,之后重点是 如何让联合类型规则只有其中某一个生效,在每一个上设置哪些禁止的属性为可选 never
from awesome-typescript.
interface Person1 {
name: string;
age?: number;
gender?: number;
}
// 想要构建成这个样子才可以满足条件
type ttt = { name: string } & ({ age: number, gender?: never } | { age?: never, gender: number })
type RP<T, K> = {
[P in keyof T]: P extends K ? T & Partial<Record<P, never>> : never
}[keyof T];
// 只能包含Keys中唯一的一个Key
type RequireExactlyOne<T, K extends keyof T> = Omit<T, K> & RP<Pick<T, K>, K>
const p1: RequireExactlyOne<Person1, 'age' | 'gender'> = {
name: "lolo",
age: 7,
};
const p2: RequireExactlyOne<Person1, 'age' | 'gender'> = {
name: "lolo",
gender: 1
};
// Error
const p3: RequireExactlyOne<Person1, 'age' | 'gender'> = {
name: "lolo",
age: 7,
gender: 1
};
from awesome-typescript.
type RequireExactlyOne<T, K extends keyof T> = K extends any
? Omit<T, K> & Partial<Record<K, never>>
: never;
from awesome-typescript.
type RequireExactlyOne<T, Keys extends keyof T, K extends keyof T = Keys> =
Omit<T, K> & Partial<Record<K, never>> | (
Keys extends any
? Omit<T, K> & Required<Pick<T, Keys>> & Partial<Record<Exclude<K, Keys>, never>>
: never
)
from awesome-typescript.
type RequireExactlyOne<T, K extends keyof T, W extends keyof T = K> = Omit<T, W> & (
K extends any ? Required<Pick<T, K>> & Partial<Record<Exclude<W, K>, undefined>> : 1
)
from awesome-typescript.
// 只能包含Keys中唯一的一个Key
type RequireExactlyOne<T, Keys extends keyof T> = Keys extends any
? Omit<T, Keys> & { [K in Keys]: never }
: never;
为啥这样不对呢?
from awesome-typescript.
// 只能包含Keys中唯一的一个Key
type RequireExactlyOne<T, Keys extends keyof T> = Keys extends any
? Omit<T, Keys> & { [K in Keys]?: never }
: never;
抱歉少了一个 可选
from awesome-typescript.
Related Issues (20)
- typescript练习第二题
- 报错的原因是返回值类型不匹配,返回值的类型是User,但T比User的范围更大
- 「重学TS 2.0 」TS 练习题第四十题 HOT 5
- #### 7.1 EmptyObject
- 「重学TS 2.0 」TS 练习题第四十一题 HOT 9
- 「重学TS 2.0 」TS 练习题第四十二题 HOT 12
- 「重学TS 2.0 」TS 练习题第四十三题 HOT 10
- 「重学TS 2.0 」TS 练习题第四十四题 HOT 4
- 「重学TS 2.0 」TS 练习题第四十五题 HOT 4
- 「重学TS 2.0 」TS 练习题第四十六题 HOT 5
- 「重学TS 2.0 」TS 练习题第四十八题 HOT 4
- readme里的电子书下载地址403了 HOT 2
- 疑惑返回类型加Partial为什么不行 HOT 1
- 第一题
- 收藏学习!!!
- 抱歉,点击错了
- 「重学TS 2.0 」TS 练习题第四十九题 HOT 4
- > ```ts
- 练习第一题 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from awesome-typescript.