-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
Open
Description
2688 - StartsWith
StartsWith follows the tradition in the medium challenges of reimplementing prototype methods, this time with String.startsWith
🎥 Video Explanation
🔢 Code
// ============= Test Cases =============
import type { Equal, Expect } from './test-utils'
type A1 = StartsWith<'abc', 'ac'>;
type B1 = false;
type C1 = Expect<Equal<A1, B1>>;
type A2 = StartsWith<'abc', 'ab'>;
type B2 = true;
type C2 = Expect<Equal<A2, B2>>;
type A3 = StartsWith<'abc', 'abc'>;
type B3 = true;
type C3 = Expect<Equal<A3, B3>>;
type A4 = StartsWith<'abc', 'abcd'>;
type B4 = false;
type C4 = Expect<Equal<A4, B4>>;
type A5 = StartsWith<'abc', ''>;
type B5 = true;
type C5 = Expect<Equal<A5, B5>>;
type A6 = StartsWith<'abc', ' '>;
type B6 = false;
type C6 = Expect<Equal<A6, B6>>;
type A7 = StartsWith<'', ''>;
type B7 = true;
type C7 = Expect<Equal<A7, B7>>;
// ============= Your Code Here =============
type StartsWith<
T extends string,
U extends string
> =
T extends `${U}${string}`
? true
: false;
// ================== NOPE ==================
// show how to debug this by replacing false with 1 and 2
type StartsWith<
T extends string,
U extends string
> =
T extends `${infer L}${U}${infer R}`
? L extends ''
? true
: false
: false;➕ More Solutions
For more video solutions to other challenges: see the umbrella list! #21338
Metadata
Metadata
Assignees
Labels
No labels