interface Array<T>{
myForEach(callback:(v:T, i:number, a:T[])=>void):void;
}
const array = [1,2,3];
[1,2,3].myForEach((v)=> {})
interface Array<T>{
myForEach(callback:(v:T, i:number, a:T[])=>void):void;
myMap<U>(callback:(value :T, index:number , array: T[]) => U): U[];
}
interface Array<T>{
myForEach(callback:(v:T, i:number, a:T[])=>void):void;
myMap<U>(callback:(value :T, index:number , array: T[]) => U): U[];
myFilter<S extends T>(callback : (v:T, i: number , a : T[]) => v is S, thisArg? : any): S[];
myFilter(callback : (v:T, i: number , a : T[]) => boolean, thisArg? : any): T[];
}
myReduce(callback : (previousValue : T, currentValue : T, currentIndex :number, array: T[]) => T):T;
myReduce(callback : (previousValue : T, currentValue : T, currentIndex :number, array: T[]) => T , initalValue : T):T;
myReduce<U>(callback : (previousValue : T, currentValue : T, currentIndex :number, array: T[]) => U ,initalValue : U): U;
flat 메서드의 매기변수 => A,D 를 제너릭으로 받음 A는 원본배열, d는 매개변수인 낮출 차원수임
FlatArray는 컨디셔널 타입을 인덱스 접근타입으로 나타냄 => depth가 -1이라면 done이라서 Arr type을 반환하고 아니라면 recur type을 반환
recur 타입은 다시 컨디셔널 타입을 실행
Arr extends ReadlonlyArray 라면? 의 의미
모든 배열은 ReadonlyArray를 extend 할 수 있으므로, 참임 (readonly T[]) 와 같은 경우임.(배열인지 아닌지 check하는 조건)
뒤에 배열은 depth를 1줄이고 다시 줄인 depth를 바탕으로 FlatArray를 호출하는 역할임
detph가 -1일경우 done 상태가되면서 Arr를 반환, 아니면 InnerArr를 계속해서 전달함.
infer가 추론되는 방법?
ReadOnlyArray 는 readonly T[]와 동일함. => 이러면 괄호를 하나 벗길 수 있도록 쉽게 추론가능
아니면 ReadOnlyArray에서 내부구현사항에 return값이 []가 붙혀서 반환되는걸로 추론?
type FlatArray<Arr, Depth extends number> = {
"done": Arr,
"recur": Arr extends ReadonlyArray<infer InnerArr>
? FlatArray<InnerArr, [-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20][Depth]>
: Arr
}[Depth extends -1 ? "done" : "recur"]; // 컨디셔녈 타입을 인덱스 접근타입으로 나타냄
interface Array<T> {
flatMap<U, This = undefined> (
callback: (this: This, value: T, index: number, array: T[]) => U | ReadonlyArray<U>,
thisArg?: This
): U[]
flat<A, D extends number = 1>(
this: A,
depth?: D
): FlatArray<A, D>[] // flat 메소드, FlatArray 를 반환함.
}
interface ReadonlyArray<T> {
flatMap<U, This = undefined> (
callback: (this: This, value: T, index: number, array: T[]) => U | ReadonlyArray<U>,
thisArg?: This
): U[]
flat<A, D extends number = 1>(
this: A,
depth?: D
): FlatArray<A, D>[]
}
interface PromiseConstructor {
readonly prototype: Promise<any>;
new <T>(executor: (resolve: (value: T | PromiseLike<T>) => void, reject: (reason?: any) => void) => void): Promise<T>;
all<T extends readonly unknown[] | []>(values: T): Promise<{ -readonly [P in keyof T]: Awaited<T[P]> }>;
race<T extends readonly unknown[] | []>(values: T): Promise<Awaited<T[number]>>;
reject<T = never>(reason?: any): Promise<T>;
resolve<T>(value: T): Promise<Awaited<T>>;
resolve<T>(value: T | PromiseLike<T>): Promise<Awaited<T>>;
}
declare var Promise: PromiseConstructor;
type Awaited<T> =
T extends null | undefined ? T : // special case for `null | undefined` when not in `--strictNullChecks` mode
T extends object & { then(onfulfilled: infer F, ...args: infer _): any } ? // `await` only unwraps object types with a callable `then`. Non-object types are not unwrapped
F extends ((value: infer V, ...args: infer _) => any) ? // if the argument to `then` is callable, extracts the first argument
Awaited<V> : // recursively unwrap the value
never : // the argument to `then` was not callable
T; // non-object or non-thenable