Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

type-challenges solution ( hard ) #42

Open
jiangshanmeta opened this issue Aug 15, 2021 · 0 comments
Open

type-challenges solution ( hard ) #42

jiangshanmeta opened this issue Aug 15, 2021 · 0 comments

Comments

@jiangshanmeta
Copy link
Owner

jiangshanmeta commented Aug 15, 2021

type-challenges

原项目地址

55・Union to Intersection

type UnionToIntersection<U> = (U extends any? (a:U)=>void : never) extends (a:infer T)=>void ? T:never

57・Get Required

type FilterRequiredKey<T,K extends keyof T> = T extends {
  [k in K]-?:T[k]
}? K :never;

type GetRequired<T> = {
  [K in keyof T as FilterRequiredKey<T,K>]:T[K]
}

59・Get Optional

type FilterOptionalKey<T,K extends keyof T> = T extends {
  [k in K]-?:T[k]
}? never:K;

type GetOptional<T> = {
  [K in keyof T as FilterOptionalKey<T,K>]:T[K]
}

89・Required Keys

type RequiredKeys<T , K = keyof T> = K extends keyof T ? 
    T extends Required<Pick<T,K>> ? K : never
    :never

90・Optional Keys

type OptionalKeys<T,K = keyof T> = K extends keyof T?
  T extends Required<Pick<T,K>>?never:K
  :never;

112・Capitalize Words

type CapitalizeWords<S extends string> = Capitalize<
  CapitalizeSeparator< CapitalizeSeparator<CapitalizeSeparator<S,' '>,','> ,'.' >
>

type CapitalizeSeparator<T extends string,S extends string> = T extends `${infer F}${S}${infer L}`?
  `${Capitalize<F>}${S}${Capitalize<CapitalizeSeparator<L,S>>}`:T

114・CamelCase

type CamelCase<S extends string,S1 = Lowercase<S>> = S1 extends `${infer F}_${infer R}`?`${F}${Capitalize<CamelCase<R>>}`:S1

147・C-printf Parser

type ControlsMap = {
  c: 'char',
  s: 'string',
  d: 'dec',
  o: 'oct',
  h: 'hex',
  f: 'float',
  p: 'pointer',
}

type ParsePrintFormat<S extends string,T extends string[] = []> = S extends `${infer F}%${infer K}${infer R}`?
  K extends keyof ControlsMap?
    ParsePrintFormat<R,[...T,ControlsMap[K]]>
    :ParsePrintFormat<R,T>
  :T

223・IsAny

type IsAny<T> = 0 extends (1&T) ? true:false

300・String to Number

type Make10<T extends any[]> = [
  ...T,
  ...T,
  ...T,
  ...T,
  ...T,
  ...T,
  ...T,
  ...T,
  ...T,
  ...T,
]

type Make1<S extends string,T extends any[] = []> = `${T['length']}` extends S?T:Make1<S,[0,...T]>

type ToTuple<S extends string,T extends any[] = []> = S extends `${infer F}${infer R}`?ToTuple<R,[...Make10<T>,...Make1<F>]>:T

type ToNumber<S extends string> = ToTuple<S>['length']

399・Tuple Filter

type FilterOut<T extends any[], F> = T extends [infer I,...infer R]?
  [I] extends [F] ? FilterOut<R,F>: [I,...FilterOut<R,F>]
  :[]

472・Tuple to Enum Object

type Tuple2Object1<T extends readonly any[]> = {
  readonly [K in T[number] as Capitalize<K>]:K
}

type Copy<T> = {
  [K in keyof T]:T[K]
}

type Tuple2Object2<T extends readonly any[],C extends any[] = []> = T extends readonly [infer F,...infer R]?
  {
    readonly [K in (F&string) as Capitalize<K> ]:C['length']
  } & Tuple2Object2<R,[...C,0]>
  :{}

type Enum<T extends readonly string[], N extends boolean = false> = N extends false ? Tuple2Object1<T>:Copy<Tuple2Object2<T>>

651・Length of String 2

type LengthOfString<S extends string,C extends number[] = []> = S extends `${infer F0}${infer F1}${infer F2}${infer F3}${infer F4}${infer F5}${infer F6}${infer F7}${infer F8}${infer F9}${infer R}`? LengthOfString<R,[...C,0,1,2,3,4,5,6,7,8,9]>
  : S extends `${infer F}${infer R}`?
    LengthOfString<R,[...C,0]>
    :C['length']

847・String Join

type Join<
  T extends any[], 
  U extends string | number,
  R extends string = ''
> = 
  T extends [infer F,...infer L]?
    L['length'] extends 0?
      `${R extends ''?'':`${R}${U}`}${F&string}`
      :Join<L,U,`${R extends ''?'':`${R}${U}`}${F&string}`>
  :R
declare function join<U extends string>(delimiter: U): <T extends string[]>(...parts: T) => Join<T,U>;

956・DeepPick

type DeepPick<T extends Record<string,any>, U extends string> = (U extends string? 
  U extends `${infer F}.${infer R}`?
    (arg:{
      [K in F]:DeepPick<T[F],R>
    })=>void
    :U extends keyof T?
      (arg:Pick<T,U>)=>void
      :(arg:unknown)=>void
  :never
  
  ) extends (arg:infer Z)=>void? Z:never;

1383・Camelize

type CamelizeWord<T extends string> = T extends `${infer F}_${infer L}`?`${F}${Capitalize<L>}`:T

type Camelize<T> = T extends any[]?
    T extends [infer F,...infer R]?
      [Camelize<F>,...Camelize<R>]
    :[]
  :
  T extends Record<string,any>?
    {
      [K in keyof T as CamelizeWord<K&string>]:Camelize<T[K]>
    }

  :T

2095・Drop String

type DropOne<S extends string,R extends string> = S extends `${infer A}${R}${infer B}`?
  DropOne<`${A}${B}`,R>
  :S

type DropString<S extends string, R extends string> = R extends `${infer F}${infer L}`?
  DropString<DropOne<S,F>,L>
  :S

2822・Split

type NormalSplit<S extends string, SEP extends string> = S extends `${infer F}${SEP}${infer R}`?[
  F,
  ...Split<R,SEP>
]:[S]

type StringToTuple<S extends string> = S extends `${infer F}${infer R}`?[F,...StringToTuple<R>]:[] 

type Split<S extends string, SEP extends string> = string extends S?
  string[]
  :SEP extends ''?
    StringToTuple<S>
    :NormalSplit<S,SEP>

2828・ClassPublicKeys

解法一:

type ClassPublicKeys<T extends any,K = keyof T> = K extends keyof T? T extends Pick<T,K>?K:never : never;

解法二:

type ClassPublicKeys<A> = keyof A

2857・IsRequiredKey

type IsRequiredKey<T, K extends keyof T> = (K extends keyof T?  
  T extends Required<Pick<T,K>>?true:false
  :never) extends true?true:false;

2949・ObjectFromEntries

解法1:

type Copy<T> = {
  [K in keyof T]:T[K]
}

type ObjectFromEntries<T extends [string,any]> = Copy<(T extends any? (arg:{
  [K in T[0]]:T[1]
})=>void:never) extends (arg:infer U)=>void ?U:never>;

解法2:

type ObjectFromEntries<T extends [string,any]> = {
  [K in T as K[0]]:K[1]
}

4037・IsPalindrome

type StringToTuple<T extends string> = T extends `${infer F}${infer R}`? [F,...StringToTuple<R>]:[];

type IsIsPalindromeArray<T extends any[]> = T extends [infer F,...infer M,infer L] ? 
  F extends L?IsIsPalindromeArray<M>:false
  :true;

type IsPalindrome<T extends string | number> = IsIsPalindromeArray<StringToTuple<`${T}`>>

5423・Intersection

type ToUnion<T> = T extends any[]? T[number]:T;

type Intersection<T extends any[],R = ToUnion<T[0]>> = T extends [infer F,...infer L]? Intersection<L,R & ToUnion<F>>:R

6141・Binary to Decimal

type BinaryToDecimal<
  S extends string,
  R extends any[] = []
> = 
  S extends `${infer F}${infer L}`?
    F extends '0'? BinaryToDecimal<L,[...R,...R]>:BinaryToDecimal<L,[...R,...R,1]>
    :R['length']

7258・Object Key Paths

type GenNode<K extends string | number,IsRoot extends boolean> = IsRoot extends true? `${K}`: `.${K}` | (K extends number? `[${K}]` | `.[${K}]`:never)

type ObjectKeyPaths<
  T extends object,
  IsRoot extends boolean = true,
  K extends keyof T = keyof T
> = 
K extends string | number ?
  GenNode<K,IsRoot> | (T[K] extends object? `${GenNode<K,IsRoot>}${ObjectKeyPaths<T[K],false>}`:never)
  :never;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant