Skip to content

Commit 5d6db72

Browse files
committed
🎉 init(first init project):
0 parents  commit 5d6db72

16 files changed

+236
-0
lines changed

‎README.md‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<!--
2+
* @Date: 2022-03-03
3+
* @Description:
4+
-->
5+
6+
7+
## type-chanllenges
8+
9+
> [type-chanllenges](https://github.com/type-challenges/type-challenges)

‎easy/11-TupleToObject.ts‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
/*
2+
* @Date: 2022-03-03
3+
* @Description: Tuple to Object
4+
*/
5+
const tuple = ["tesla", "model 3", "model X", "model Y"] as const;
6+
7+
type TupleToObject<T extends readonly any[]> = {
8+
[K in T[number]]: K;
9+
};
10+
11+
type result = TupleToObject<typeof tuple>; // expected { tesla: 'tesla', 'model 3': 'model 3', 'model X': 'model X', 'model Y': 'model Y'}
12+
13+
export {};

‎easy/14-FirstOfArray.ts‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/*
2+
* @Date: 2022-03-03
3+
* @Description:First of Array
4+
*/
5+
type arr1 = ["a", "b", "c"];
6+
type arr2 = [3, 2, 1];
7+
8+
type First<T extends any[]> = T extends [] ? never : T[0];
9+
10+
type head1 = First<arr1>; // expected to be 'a'
11+
type head2 = First<arr2>; // expected to be 3\
12+
13+
const test: head2 = 3;
14+
15+
export {};

‎easy/18-LengthOfTuple.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* @Date: 2022-03-03
3+
* @Description: Length of Tuple
4+
*/
5+
type tesla = ["tesla", "model 3", "model X", "model Y"];
6+
type spaceX = [
7+
"FALCON 9",
8+
"FALCON HEAVY",
9+
"DRAGON",
10+
"STARSHIP",
11+
"HUMAN SPACEFLIGHT"
12+
];
13+
14+
type Length<K extends readonly any[]> = K["length"];
15+
16+
type teslaLength = Length<tesla>; // expected 4
17+
type spaceXLength = Length<spaceX>; // expected 5
18+
19+
export {};

‎easy/189-EasyAwaited.ts‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* @Date: 2022-03-03
3+
* @Description: EasyAwaited
4+
*/
5+
6+
type IUser = {
7+
name: string;
8+
age: number;
9+
};
10+
11+
type PromiseType = Promise<IUser>;
12+
13+
type EasyAwaited<T extends Promise<unknown>> = T extends Promise<infer R>
14+
? R extends Promise<unknown>
15+
? EasyAwaited<R>
16+
: R
17+
: never;
18+
19+
type T1 = EasyAwaited<PromiseType>;

‎easy/268-If.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/*
2+
* @Date: 2022-03-03
3+
* @Description: If
4+
*/
5+
6+
type If<T extends boolean, K, U> = T extends true ? K : U;
7+
8+
type A = If<true, "a", "b">; // expected to be 'a'
9+
type B = If<false, "a", "b">; // expected to be 'b'
10+
11+
export {};

‎easy/3057-Push.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/*
2+
* @Date: 2022-03-03
3+
* @Description: Push
4+
*/
5+
6+
type Push<T extends readonly any[], U> = [...T, U];
7+
8+
type Result = Push<[1, 2], "3">; // [1, 2, '3']
9+
10+
export {};

‎easy/3060-Unshift.ts‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/*
2+
* @Date: 2022-03-03
3+
* @Description: Unshift
4+
*/
5+
6+
type Unshift<T extends unknown[], U> = [U, ...T];
7+
8+
type Result = Unshift<[1, 2], 0>; // [0, 1, 2,]
9+
10+
export {};

‎easy/3312-Parameters.ts‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
/*
2+
* @Date: 2022-03-03
3+
* @Description: Parameters
4+
*/
5+
6+
type MyParameters<T> = T extends (...args: infer R) => any ? R : any;
7+
8+
type F1 = (x: number) => string;
9+
10+
type Result = MyParameters<F1>; // [x:number]
11+
12+
export {};

‎easy/4-Pick.ts‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
/*
2+
* @Date: 2022-03-03
3+
* @Description: Pick
4+
*/
5+
interface Todo {
6+
title: string;
7+
description: string;
8+
completed: boolean;
9+
}
10+
11+
type MyPick<T, K extends keyof T> = {
12+
[P in K]: T[K];
13+
};
14+
15+
type TodoPreview = MyPick<Todo, "title" | "completed">;
16+
17+
const todo: TodoPreview = {
18+
title: "Clean room",
19+
completed: false,
20+
};
21+
22+
export {};

0 commit comments

Comments
 (0)