Giter Club home page Giter Club logo

type-challenges's Introduction

Hi there πŸ‘‹

Github Stats Top Langs

type-challenges's People

Contributors

alexsey avatar antfu avatar bre30kra69cs avatar ch3cknull avatar cutefcc avatar dogdriip avatar dqn avatar g-plane avatar github-actions[bot] avatar jazelly avatar jiangshanmeta avatar jiatln avatar jonghwanwon avatar kawamataryo avatar kynefuk avatar leelejia avatar likui628 avatar majorlift avatar mistlog avatar qianxi0410 avatar sinoon avatar suica avatar talljack avatar tenkirin avatar tisou1 avatar uid11 avatar xianshenglu avatar xjq7 avatar yash-singh1 avatar yrming avatar

type-challenges's Issues

7 - Readonly

type MyReadonly<T> = {
  readonly [P in keyof T]: T[P]
}

43 - Exclude

type MyExclude<T, K> = T extends K ? never : T;

296 - Permutation

type Permutation<T, K = T> = [T] extends [never] ?
  [] : 
  K extends K?
    [K, ...Permutation<Exclude<T, K>>] :
    never;

2 - Get Return Type

type MyReturnType<T extends (...args: any[]) => any> = T extends (...args: any[]) => infer R ? R : any

11 - Tuple to Object

type TupleToObject<T extends readonly PropertyKey[]> = {
  [P in T[number]]: P;
}

898 - Includes

type Includes<T extends readonly unknown[], U> =
  T extends [infer first, ...infer rest] ?
    Equal<first, U> extends true ?
      true :
      Includes<rest, U> :
      false

14 - First of Array

type First<T extends any[]> = T extends [] ? never : T[0]

type First<T extends any[]> = T["length"] extends 0? never : T[0]

type First<T extends any[]> = T extends [infer A, ...any[]] ? A : never

16 - Pop

type Pop<T extends any[]> = T extends [...infer I, infer _] ? I : [];

119 - ReplaceAll

type ReplaceAll<S extends string, From extends string, To extends string> = From extends ''
  ? S
  : S extends `${infer R1}${From}${infer R2}`
  ? `${R1}${To}${ReplaceAll<R2, From, To>}`
  : S

268 - If

type If<C extends boolean, T, F> = C extends true ? T : F;

3057 - Push

type Push<T extends unknown[], U> = [...T, U]

3312 - Parameters

type MyParameters<T extends (...args: any[]) => any> = T extends (...args: infer S) => any ? S : any 

20 - Promise.all

declare function PromiseAll<T extends any[]>(
  values: readonly [...T]
): Promise<{ [key in keyof T]: Awaited<T[key]> }>

533 - Concat

type Tupple = readonly unknown[];

type Concat<T extends Tupple, U extends Tupple> = [...T, ...U];

116 - Replace

type Replace<S extends string, From extends string, To extends string> = 
  From extends '' ?
    S :
    S extends `${infer Before}${From}${infer After}` ?
      `${Before}${To}${After}` :
      S;

189 - Awaited

type MyAwaited<T> = T extends PromiseLike<infer V>
    ? V extends PromiseLike<any>
      ? MyAwaited<V>
      : V
    : never

3 - Omit

type MyOmit<T, K extends keyof T> = {[P in Exclude<keyof T, K>]: T[P]};

110 - Capitalize

type MyCapitalize<S extends string> = S extends `${infer first}${infer rest}` ? `${Uppercase<first>}${rest}`: S;

108 - Trim

type Whitespace = '\n' | '\t' | ' ';
type Trim<S extends string> = S extends `${Whitespace}${infer U}` | `${infer U}${Whitespace}` ? Trim<U> : S;

106 - Trim Left

type Whitespace = '\n' | '\t' | ' ';
type TrimLeft<S extends string> = S extends `${Whitespace}${infer U}` ? TrimLeft<U> : S;

8 - Readonly 2

type MyReadonly2<T, Kγ€€extends keyof T = keyof T> = Omit<T, K> & Readonly<Pick<T, K>>;

4 - Pick

type MyPick<T, K extends keyof T> = {
  [P in K]: T[P]
}

12 - Chainable Options

type Chainable<T = {}> = {
  option<K extends string, V>(
    key: K extends keyof T ? never : K,
    value: V
  ): Chainable<Omit<T, K> & Record<K, V>>;
  get(): T;
}

191 - Append Argument

type AppendArgument<Fn extends Function, A> = 
  Fn extends (...args: infer R) => infer T ?
    (...args: [...R, A]) => T :
    unknown;

298 - Length of String

type LengthOfString<S extends string, T extends string[] = []> = 
  S extends `${infer A}${infer B}` ?
    LengthOfString<B, [A, ...T]> :
    T["length"];

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.