这是indexloc提供的服务,不要输入任何密码
Skip to content

2688 - StartsWith (🎥 Video Explanation and Solution) #23721

@dimitropoulos

Description

@dimitropoulos

2688 - StartsWith

StartsWith follows the tradition in the medium challenges of reimplementing prototype methods, this time with String.startsWith

🎥 Video Explanation

Release Date: 2023-02-18 19:00 UTC

StartsWith

🔢 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

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions