Skip to content

Commit

Permalink
中等类型题
Browse files Browse the repository at this point in the history
  • Loading branch information
Chenmin926 committed Sep 23, 2023
1 parent ad6feac commit 54c64bc
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@

/* _____________ 你的代码 _____________ */

type MyReturnType<T> = any
type MyReturnType<T extends (...args: any[]) => any> = T extends (...args: any[]) => infer R ? R : never

/* _____________ 测试用例 _____________ */
import type { Equal, Expect } from '@type-challenges/utils'
Expand Down
4 changes: 3 additions & 1 deletion playground/medium/00003-medium-shi-xian-omit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@

/* _____________ 你的代码 _____________ */

type MyOmit<T, K> = any
type MyOmit<T, K extends keyof T> = {
[P in keyof T as P extends K ? never : P]: T[P]
}

/* _____________ 测试用例 _____________ */
import type { Equal, Expect } from '@type-challenges/utils'
Expand Down
13 changes: 11 additions & 2 deletions playground/medium/00008-medium-dui-xiang-bu-fen-shu-xing-zhi-du.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,17 @@
*/

/* _____________ 你的代码 _____________ */

type MyReadonly2<T, K> = any
/**
* K extends keyof T = keyof T 泛型设置默认参数
* 将属性分为两类 一类要是readonly
* 一类不用
* 然后用交叉类型合并为一个
*/
type MyReadonly2<T, K extends keyof T = keyof T> = {
readonly [P in keyof T as P extends K ? P : never]: T[P];
} & {
[O in keyof T as O extends K ? never : O]: T[O]
}

/* _____________ 测试用例 _____________ */
import type { Alike, Expect } from '@type-challenges/utils'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,13 @@
*/

/* _____________ 你的代码 _____________ */

type DeepReadonly<T> = any
/**
* 使用递归进行解决
* keyof T[P] extends never 相当于判断该属性是否为简单值
*/
type DeepReadonly<T> = {
readonly [P in keyof T]: keyof T[P] extends never ? T[P] : DeepReadonly<T[P]>
}

/* _____________ 测试用例 _____________ */
import type { Equal, Expect } from '@type-challenges/utils'
Expand Down Expand Up @@ -69,6 +74,8 @@ type X1 = {
}
}

type T1 = X1 extends object ? true : false

type X2 = { a: string } | { b: number }

type Expected1 = {
Expand Down

0 comments on commit 54c64bc

Please sign in to comment.