Open22

type-challenges

shikatanshikatan

18 - Length of Tuple

回答

type Length<T extends readonly unknown[]> = T['length']
shikatanshikatan

533 - Concat

回答

type Concat<T extends readonly unknown[], U extends readonly unknown[]> = [...T, ...U]
shikatanshikatan

898 - Includes

回答

type Includes<T extends readonly any[], U> = T extends [infer A, ...infer B]
	? (<C>() => C extends A ? 1 : 0) extends (<C>() => C extends U ? 1 : 0) ? true : Includes<B, U>
	: false
shikatanshikatan

3312 - Parameters

回答

type MyParameters<T extends (...args: any[]) => unknown> = T extends (...args: infer A) => unknown ? A : never
shikatanshikatan

3 - Omit

回答

type MyOmit<T, K extends keyof T> = { [A in keyof T as A extends K ? never : A]: T[A] }
shikatanshikatan

9 - Deep Readonly

回答

type DeepReadonly<T> = { readonly [A in keyof T]: T[A] extends (...args: any[]) => unknown ? T[A] : DeepReadonly<T[A]> }

オブジェクトと配列のみ readonly にしたい。

用意されているケースの () => 22 以外の型は mapped type でオブジェクトに変換されないっぽい。

This mapped type returns a primitive type, not an object type.
https://github.com/microsoft/TypeScript/wiki/FAQ#common-bugs-that-arent-bugs

() => 22 だけ除外する mapped type でオブジェクトと配列のみ readonly を表現できた。

shikatanshikatan

12 - Chainable Options

回答

type Chainable<A = object> = {
	option<B extends string, C>(
		key: B extends keyof A ? never : B,
		value: C,
	): Chainable<{ [D in keyof A as D extends B ? never : D]: A[D] } & { [_ in B]: C }>
	get(): A
}