Skip to content

Commit b48c795

Browse files
committed
add 2 ts medium challenges
1 parent 8bfa326 commit b48c795

File tree

2 files changed

+116
-0
lines changed

2 files changed

+116
-0
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
2070 - Drop Char
3+
-------
4+
by CaptainOfPhB (@CaptainOfPhB) #medium #template-literal #infer
5+
6+
### Question
7+
8+
Drop a specified char from a string.
9+
10+
For example:
11+
12+
```ts
13+
type Butterfly = DropChar<' b u t t e r f l y ! ', ' '> // 'butterfly!'
14+
```
15+
16+
> View on GitHub: https://tsch.js.org/2070
17+
*/
18+
19+
/* _____________ Your Code Here _____________ */
20+
21+
type DropChar<S, C> = S extends `${infer H}${infer T}`
22+
? `${H extends C ? "" : H}${DropChar<T, C>}`
23+
: S;
24+
25+
type Butterfly = DropChar<" b u t t e r f l y ! ", " ">; // 'butterfly!'
26+
// ^?
27+
28+
/* _____________ Test Cases _____________ */
29+
import type { Equal, Expect } from "@type-challenges/utils";
30+
31+
type cases = [
32+
// @ts-expect-error
33+
Expect<Equal<DropChar<"butter fly!", "">, "butterfly!">>,
34+
Expect<Equal<DropChar<"butter fly!", " ">, "butterfly!">>,
35+
Expect<Equal<DropChar<"butter fly!", "!">, "butter fly">>,
36+
Expect<Equal<DropChar<" butter fly! ", " ">, "butterfly!">>,
37+
Expect<Equal<DropChar<" b u t t e r f l y ! ", " ">, "butterfly!">>,
38+
Expect<Equal<DropChar<" b u t t e r f l y ! ", "b">, " u t t e r f l y ! ">>,
39+
Expect<Equal<DropChar<" b u t t e r f l y ! ", "t">, " b u e r f l y ! ">>
40+
];
41+
42+
/* _____________ Further Steps _____________ */
43+
/*
44+
> Share your solutions: https://tsch.js.org/2070/answer
45+
> View solutions: https://tsch.js.org/2070/solutions
46+
> More Challenges: https://tsch.js.org
47+
*/
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
2595 - PickByType
3+
-------
4+
by jiangshan (@jiangshanmeta) #medium #object
5+
6+
### Question
7+
8+
From `T`, pick a set of properties whose type are assignable to `U`.
9+
10+
For Example
11+
12+
```typescript
13+
type OnlyBoolean = PickByType<{
14+
name: string
15+
count: number
16+
isReadonly: boolean
17+
isEnable: boolean
18+
}, boolean> // { isReadonly: boolean; isEnable: boolean; }
19+
```
20+
21+
> View on GitHub: https://tsch.js.org/2595
22+
*/
23+
24+
/* _____________ Your Code Here _____________ */
25+
26+
type PickByType<T, U> = {
27+
[K in keyof T as T[K] extends U ? K : never]: T[K];
28+
};
29+
30+
type OnlyBoolean = PickByType<
31+
{
32+
name: string;
33+
count: number;
34+
isReadonly: boolean;
35+
isEnable: boolean;
36+
},
37+
boolean
38+
>;
39+
40+
const onlyBoolean: OnlyBoolean = { isEnable: false, isReadonly: true };
41+
// ^?
42+
43+
/* _____________ Test Cases _____________ */
44+
import type { Equal, Expect } from "@type-challenges/utils";
45+
46+
interface Model {
47+
name: string;
48+
count: number;
49+
isReadonly: boolean;
50+
isEnable: boolean;
51+
}
52+
53+
type cases = [
54+
Expect<
55+
Equal<
56+
PickByType<Model, boolean>,
57+
{ isReadonly: boolean; isEnable: boolean }
58+
>
59+
>,
60+
Expect<Equal<PickByType<Model, string>, { name: string }>>,
61+
Expect<Equal<PickByType<Model, number>, { count: number }>>
62+
];
63+
64+
/* _____________ Further Steps _____________ */
65+
/*
66+
> Share your solutions: https://tsch.js.org/2595/answer
67+
> View solutions: https://tsch.js.org/2595/solutions
68+
> More Challenges: https://tsch.js.org
69+
*/

0 commit comments

Comments
 (0)