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

12 - Chainable Options (🎥 Video Explanation and Solution) #21548

@dimitropoulos

Description

@dimitropoulos

12 - Chainable Options

Thankfully JavaScript APIs that rely on method Chaining (where you go a.b().c().d().e().f()) have fallen out of fashion, but even if you dislike this style of API, this is one of the first challenges that should make you stand back and say "WOW! YOU CAN DO THAT?!?!". Enjoy.

🎥 Video Explanation

Release Date: 2023-01-20 19:00 UTC

Chainable Options

🔢 Code

// ============= Test Cases =============
import type { Alike, Expect } from './test-utils'

declare const chainable: Chainable

const a1 = chainable
  .option('foo', 123)
  .option('bar', { value: 'Hello World' })
  .option('name', 'type-challenges')
  .get();
type B1 = {
  foo: number;
  bar: {
    value: string;
  };
  name: string;
}
type C1 = Expect<Alike<typeof a1, B1>>;

const a2 = chainable
  .option('name', 'another name')
  // @ts-expect-error(2345)
  .option('name', 'last name')
  .get();
type B2 = {
  name: string;
};
type C2 = Expect<Alike<typeof a2, B2>>;

const a3 = chainable
  .option('name', 'another name')
  .option('name', 123)
  .get();
type B3 = {
  name: number;
};
type C3 = Expect<Alike<typeof a3, B3>>;

// ============= Your Code Here =============
type Chainable = {
  option(key: string, value: any): any;
  get(): any;
}

type Chainable<T = object> = {
  option(key: string, value: any): any;
  get(): T; // type return
}

type Chainable<T = object> = {
  option(
    key: string,
    value: any,
  ): Chainable; // type return
  get(): T;
}

type Chainable<T = object> = {
  option(
    key: K, // give K a type
    value: V // give V a type
  ): Chainable<T & Record<K, V>>; // join T and Record
  get(): T;
}

type Chainable<T = object> = {
  option<K extends PropertyKey, V>( // provide generic
    key: K,
    value: V
  ): Chainable<T & Record<K, V>>;
  get(): T;
}

type Chainable<T = object> = {
  option<K extends PropertyKey, V>(
    key: K extends keyof T ? never : K, // start filtering K
    value: V
  ): Chainable<T & Record<K, V>>;
  get(): T;
}

type Chainable<T = object> = {
  option<K extends PropertyKey, V>(
    key: K extends keyof T
         ? (V extends T[K] ? never : K) // finish filter
         : K,
    value: V
  ): Chainable<T & Record<K, V>>;
  get(): T;
}

type Chainable<T = object> = {
  option<K extends PropertyKey, V>(
    key: K extends keyof T
         ? (V extends T[K] ? never : K)
         : K,
    value: V
  ): Chainable<Omit<T, K> & Record<K, V>>; // Omit K from T
  get(): T;
}

➕ 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