Skip to content

Commit 2f4a8f3

Browse files
committed
add five ts medium challenges
1 parent b48c795 commit 2f4a8f3

File tree

5 files changed

+285
-0
lines changed

5 files changed

+285
-0
lines changed
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
2693 - EndsWith
3+
-------
4+
by jiangshan (@jiangshanmeta) #medium #template-literal
5+
6+
### Question
7+
8+
Implement `EndsWith<T, U>` which takes two exact string types and returns whether `T` ends with `U`
9+
10+
For example:
11+
12+
```typescript
13+
type a = EndsWith<'abc', 'bc'> // expected to be true
14+
type b = EndsWith<'abc', 'abc'> // expected to be true
15+
type c = EndsWith<'abc', 'd'> // expected to be false
16+
```
17+
18+
> View on GitHub: https://tsch.js.org/2693
19+
*/
20+
21+
/* _____________ Your Code Here _____________ */
22+
23+
type EndsWith<T extends string, U extends string> = T extends `${infer _}${U}`
24+
? true
25+
: false;
26+
27+
/* _____________ Test Cases _____________ */
28+
import type { Equal, Expect } from "@type-challenges/utils";
29+
30+
type cases = [
31+
Expect<Equal<EndsWith<"abc", "bc">, true>>,
32+
Expect<Equal<EndsWith<"abc", "abc">, true>>,
33+
Expect<Equal<EndsWith<"abc", "d">, false>>,
34+
Expect<Equal<EndsWith<"abc", "ac">, false>>,
35+
Expect<Equal<EndsWith<"abc", "">, true>>,
36+
Expect<Equal<EndsWith<"abc", " ">, false>>
37+
];
38+
39+
/* _____________ Further Steps _____________ */
40+
/*
41+
> Share your solutions: https://tsch.js.org/2693/answer
42+
> View solutions: https://tsch.js.org/2693/solutions
43+
> More Challenges: https://tsch.js.org
44+
*/
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
/*
2+
2759 - RequiredByKeys
3+
-------
4+
by jiangshan (@jiangshanmeta) #medium #object
5+
6+
### Question
7+
8+
Implement a generic `RequiredByKeys<T, K>` which takes two type argument `T` and `K`.
9+
10+
`K` specify the set of properties of `T` that should set to be required. When `K` is not provided, it should make all properties required just like the normal `Required<T>`.
11+
12+
For example
13+
14+
```typescript
15+
interface User {
16+
name?: string
17+
age?: number
18+
address?: string
19+
}
20+
21+
type UserRequiredName = RequiredByKeys<User, 'name'> // { name: string; age?: number; address?: string }
22+
23+
```
24+
25+
> View on GitHub: https://tsch.js.org/2759
26+
*/
27+
28+
/* _____________ Your Code Here _____________ */
29+
30+
type RequiredByKeys<T, K extends keyof T = any> = Required<
31+
{
32+
[P in keyof T as P extends K ? P : never]: T[P];
33+
}
34+
> &
35+
{
36+
[P in keyof T as P extends K ? never : P]: T[P];
37+
} extends infer M
38+
? {
39+
[P in keyof M]: M[P];
40+
}
41+
: Required<T>;
42+
43+
/* _____________ Test Cases _____________ */
44+
import type { Equal, Expect } from "@type-challenges/utils";
45+
46+
interface User {
47+
name?: string;
48+
age?: number;
49+
address?: string;
50+
}
51+
52+
interface UserRequiredName {
53+
name: string;
54+
age?: number;
55+
address?: string;
56+
}
57+
58+
interface UserRequiredNameAndAge {
59+
name: string;
60+
age: number;
61+
address?: string;
62+
}
63+
64+
type cases = [
65+
Expect<Equal<RequiredByKeys<User, "name">, UserRequiredName>>,
66+
Expect<Equal<RequiredByKeys<User, "name" | "age">, UserRequiredNameAndAge>>,
67+
Expect<Equal<RequiredByKeys<User>, Required<User>>>,
68+
// @ts-expect-error
69+
Expect<Equal<RequiredByKeys<User, "name" | "unknown">, UserRequiredName>>
70+
];
71+
72+
/* _____________ Further Steps _____________ */
73+
/*
74+
> Share your solutions: https://tsch.js.org/2759/answer
75+
> View solutions: https://tsch.js.org/2759/solutions
76+
> More Challenges: https://tsch.js.org
77+
*/
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
2793 - Mutable
3+
-------
4+
by jiangshan (@jiangshanmeta) #medium #readonly #object-keys
5+
6+
### Question
7+
8+
Implement the generic ```Mutable<T>``` which makes all properties in ```T``` mutable (not readonly).
9+
10+
For example
11+
12+
```typescript
13+
interface Todo {
14+
readonly title: string
15+
readonly description: string
16+
readonly completed: boolean
17+
}
18+
19+
type MutableTodo = Mutable<Todo> // { title: string; description: string; completed: boolean; }
20+
21+
```
22+
23+
> View on GitHub: https://tsch.js.org/2793
24+
*/
25+
26+
/* _____________ Your Code Here _____________ */
27+
28+
type Mutable<T extends { [key: string]: any }> = {
29+
-readonly [K in keyof T]: T[K];
30+
};
31+
32+
/* _____________ Test Cases _____________ */
33+
import type { Equal, Expect } from "@type-challenges/utils";
34+
35+
interface Todo1 {
36+
title: string;
37+
description: string;
38+
completed: boolean;
39+
meta: {
40+
author: string;
41+
};
42+
}
43+
44+
type List = [1, 2, 3];
45+
46+
type cases = [
47+
Expect<Equal<Mutable<Readonly<Todo1>>, Todo1>>,
48+
Expect<Equal<Mutable<Readonly<List>>, List>>
49+
];
50+
51+
type errors = [
52+
// @ts-expect-error
53+
Mutable<"string">,
54+
// @ts-expect-error
55+
Mutable<0>
56+
];
57+
58+
/* _____________ Further Steps _____________ */
59+
/*
60+
> Share your solutions: https://tsch.js.org/2793/answer
61+
> View solutions: https://tsch.js.org/2793/solutions
62+
> More Challenges: https://tsch.js.org
63+
*/
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/*
2+
2852 - OmitByType
3+
-------
4+
by jiangshan (@jiangshanmeta) #medium #object
5+
6+
### Question
7+
8+
From ```T```, pick a set of properties whose type are not assignable to ```U```.
9+
10+
For Example
11+
12+
```typescript
13+
type OmitBoolean = OmitByType<{
14+
name: string
15+
count: number
16+
isReadonly: boolean
17+
isEnable: boolean
18+
}, boolean> // { name: string; count: number }
19+
```
20+
21+
> View on GitHub: https://tsch.js.org/2852
22+
*/
23+
24+
/* _____________ Your Code Here _____________ */
25+
26+
type OmitByType<T extends { [key: string]: any }, U> = {
27+
[K in keyof T as T[K] extends U ? never : K]: T[K];
28+
};
29+
30+
/* _____________ Test Cases _____________ */
31+
import type { Equal, Expect } from "@type-challenges/utils";
32+
33+
interface Model {
34+
name: string;
35+
count: number;
36+
isReadonly: boolean;
37+
isEnable: boolean;
38+
}
39+
40+
type cases = [
41+
Expect<Equal<OmitByType<Model, boolean>, { name: string; count: number }>>,
42+
Expect<
43+
Equal<
44+
OmitByType<Model, string>,
45+
{ count: number; isReadonly: boolean; isEnable: boolean }
46+
>
47+
>,
48+
Expect<
49+
Equal<
50+
OmitByType<Model, number>,
51+
{ name: string; isReadonly: boolean; isEnable: boolean }
52+
>
53+
>
54+
];
55+
56+
/* _____________ Further Steps _____________ */
57+
/*
58+
> Share your solutions: https://tsch.js.org/2852/answer
59+
> View solutions: https://tsch.js.org/2852/solutions
60+
> More Challenges: https://tsch.js.org
61+
*/
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
3062 - Shift
3+
-------
4+
by jiangshan (@jiangshanmeta) #medium #array
5+
6+
### Question
7+
8+
Implement the type version of ```Array.shift```
9+
10+
For example
11+
12+
```typescript
13+
type Result = Shift<[3, 2, 1]> // [2, 1]
14+
```
15+
16+
> View on GitHub: https://tsch.js.org/3062
17+
*/
18+
19+
/* _____________ Your Code Here _____________ */
20+
21+
type Shift<T extends any[]> = T extends [infer _, ...infer Rest] ? Rest : [];
22+
23+
/* _____________ Test Cases _____________ */
24+
import type { Equal, Expect } from "@type-challenges/utils";
25+
26+
type cases = [
27+
// @ts-expect-error
28+
Shift<unknown>,
29+
Expect<Equal<Shift<[]>, []>>,
30+
Expect<Equal<Shift<[1]>, []>>,
31+
Expect<Equal<Shift<[3, 2, 1]>, [2, 1]>>,
32+
Expect<Equal<Shift<["a", "b", "c", "d"]>, ["b", "c", "d"]>>
33+
];
34+
35+
/* _____________ Further Steps _____________ */
36+
/*
37+
> Share your solutions: https://tsch.js.org/3062/answer
38+
> View solutions: https://tsch.js.org/3062/solutions
39+
> More Challenges: https://tsch.js.org
40+
*/

0 commit comments

Comments
 (0)