type Pop<T extends any[]> = T extends []
? []
: T extends [...infer Rest, any]
? Rest
: never
Solution by eunsukimme #35123
type Pop<T extends any[]> = T extends [...infer F, infer L] ? [...F] : never;
Solution by raeyoung-kim #35001
type Pop<T extends any[]> = T extends never[] ? [] : T extends [...infer R, any] ? R: never
Solution by 56aiden90 #34956
// your answers
type Pop<T extends any[]> = T extends [...infer P, any] ? P : T;
Solution by zeyuanHong0 #34803
type Pop<T extends any[]> = T extends [...infer R, infer _] ? R : []
Solution by YBFJ #34682
type Pop<T extends any[]> = T extends [...infer A,infer B] ? [...A] : []
Solution by devshinthant #34566
type Pop<T extends unknown[]> = T extends [] ? [] : (T extends [...infer P, infer R] ? P : never);
对于[]的情况需要进行特殊判定,尝试过多种解决方案,不加[]做特殊判定的分支情况下,在泛型中采用any[]和unknown[]都无法通过。
Solution by wxh-cyber #34490
// your answers
type Pop<T extends any[]> = T extends [] ? [] : T extends [...infer P, infer _X] ? P : never
Solution by gobielJonathan #34472
type Pop<T extends any[]> = T extends [...infer R, infer L] ? R : [];
Solution by ktim816 #34444
type Pop<T extends any[]> = T extends [infer A, infer B, ...infer Rest]
? Rest['length'] extends 0 ? [A] : [A, ...Pop<[B, ...Rest]>]
: []
Solution by rookie-luochao #34388
// 你的答案
type Pop<T extends any[]> = T extends [...infer A, infer B] ? A : []
Solution by W-fitTiger #34256
이 챌린지에는 TypeScript 4.0 사용이 권장됩니다.
배열 T
를 사용해 마지막 요소를 제외한 배열을 반환하는 제네릭 Pop<T>
를 구현합니다.
예시
type arr1 = ["a", "b", "c", "d"];
type arr2 = [3, 2, 1];
type re1 = Pop<arr1>; // expected to be ['a', 'b', 'c']
type re2 = Pop<arr2>; // expected to be [3, 2]
앞선 문제였던 Last 문제와 아주 유사한 문제였습니다. 타입을 추론하는 infer를 사용하여 해결합니다.
type Pop<T extends any[]> = T extends [...infer Rest, infer Last]
? [...Rest]
: [];
/* _____________ 테스트 케이스 _____________ */
import type { Equal, Expect } from "@type-challenges/utils";
type cases = [
Expect<Equal<Pop<[3, 2, 1]>, [3, 2]>>,
Expect<Equal<Pop<["a", "b", "c", "d"]>, ["a", "b", "c"]>>,
Expect<Equal<Pop<[]>, []>>
];
Solution by adultlee #34156
type Pop<T extends any[]> = T extends [...infer ANY, infer L] ? ANY : T;
Solution by G00syara #34041
type Pop<T extends any[]> = T['length'] extends 0 ? T : T extends [...infer P, any] ? P : never
Solution by ouzexi #33990
type Pop<T extends any[]> = T extends [...args: infer R, any] ? R : []
Solution by Mumujianguang #33956
type Pop<T extends any[]> = T extends [...infer R, infer S] ? R : [];
Solution by ckdwns9121 #33953
type Pop<T extends any[]> = T extends [...infer Rest, infer _] ? Rest : []
Solution by notsecret32 #33892
type Pop<T extends Array<unknown>> = T extends [...infer R, infer P] ? R : never
type Push<T extends Array<unknown>, U> = [...T, U]
type Shift<T extends Array<unknown>> = T extends [infer R, ...infer P] ? P : never
type Unshift<T extends Array<unknown>, U> = [U, ...T]
Solution by Danny101201 #33833
type Pop<T extends any[]> = T extends [...infer Rest, any] ? Rest : []
Solution by laplace1009 #33719
type Pop<T extends any[]> = T['length'] extends 0 ? [] : T extends [...infer P, infer L] ? (L extends never ? [] : P) : never;
Solution by nupthale #33582
// 你的答案
type Pop<T extends any[]> = T extends [] ? [] : T extends [...infer F, infer L] ? F : never
Solution by shaobeichen #33375
type Pop<T extends any[]> = T extends [] ? []
: T extends [...infer R, infer L] ? R : never
Solution by naoxin20040419 #33364
type Pop<T extends any[]> = T extends [...infer P, unknown] ? P : []
Solution by loevray #33260
type Pop<T extends Array<any>> = T extends [...infer R, any] ? R : never;
type push<T extends any[],K> = [...T,K];
type shift<T> = T extends [any,...infer R] ? R : never;
type unshift<T extends any[],R extends any[]|string|number> = R extends Array<any> ?[ ...R ,...T] : [R,...T];
Solution by fffqwhy #32955
type Pop<T extends any[]> = T extends [...infer R, infer _] ? [...R] : [];
type Push<T extends any[], V> = [...T, V];
type Shift<T extends any[]> = T extends [infer _, ...infer R] ? R : never;
type Unshift<T extends any[], V> = [V, ...T];
Solution by juffyduan #32946
// 你的答案
type Pop<T extends any[],R extends any[] = []> = T extends [infer F,...infer O] ? O extends [] ? R : Pop<O,[...R,F]>
:[]
Solution by walker-hzx #32936
type Pop<T extends any[]> = T extends [...infer S, infer U] ? S : never;
Solution by CAN1177 #32869
// 解答をここに記入
type Pop<T extends any[]> = T extends []
? []
: T extends [...infer A, any]
? [...A]
: never;
infer A
に集約し返却。Solution by Yasunori-aloha #32825
type Pop<T extends any[]> = T['length'] extends 0 ? [] : T extends [...infer F, infer L] ? F : never;
Solution by ZhipengYang0605 #32799
type Pop<T> = T extends [...infer R , infer _] ? [...R] : [];
Solution by malyanoff #32780