03060-easy-unshift

Back

type Unshift<T, U> = T extends any[] ? [U, ...T]: [];

Solution by dailyco #35207

type Unshift<T extends any[], U> = [U, ...T];

Solution by IsaacYouKorea #35206

type Unshift<T extends unknown[], P> = [P, ...T]

Solution by ClarityOfMind #35003

type Unshift<T extends any[], U> = [U, ...T];

Solution by raeyoung-kim #34946

type Unshift<T extends unknown[], U> = T extends never[] ? [U] : [U, ...T]

Solution by 56aiden90 #34887

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

Solution by eunsukimme #34817

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

Solution by Yuriy113 #34736

// your answers
type Unshift<T extends unknown[], U> = [U, ...T]

Solution by zeyuanHong0 #34726

type Unshift<T extends any[], U> = T["length"] extends 0 ? [U] : [U, ...T];

Solution by lephuthuc2001 #34708

type Unshift<T extends readonly any[], U> = [U, ...T]
image

Solution by Git-I985 #34706

type Unshift<T, U> = T extends any[] ? [U, ...T] : T

Solution by nathan2slime #34664

type Unshift<T extends unknown[], U> = [U, ...T]
// your answers

Solution by Rustamaha #34555

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

Solution by devshinthant #34550

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

Solution by binhdv155127 #34474

// ์—ฌ๊ธฐ ํ’€์ด๋ฅผ ์ž…๋ ฅํ•˜์„ธ์š”
type Unshift<T extends readonly unknown[], U> = [U, ...T];

Solution by LeeKangHyun #34465

We should add U at array T's first place. So, declare a new array(type) and add U. And, spread T's elements.

// your answers
type Unshift<T extends any[], U> = [U, ...T]

Solution by dev-jaemin #34459

type Unshift<T extends readonly any[], U> = [U, ...T];

Solution by ktim816 #34429

type Unshift<T extends any[], U> = [U, ...T]

Solution by bkdragon0228 #34426

// your answers
type Unshift<T extends any[], U> = [U, ...T]

Solution by gobielJonathan #34400

type Unshift<T extends any[], U> = [U, ...T];

Solution by wxh-cyber #34370

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

Solution by souzaramon #34318

// ไฝ ็š„็ญ”ๆกˆ

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

Solution by W-fitTiger #34250

๋ฌธ์ œ์„ค๋ช…

Array.unshift์˜ ํƒ€์ž… ๋ฒ„์ „์„ ๊ตฌํ˜„ํ•˜์„ธ์š”.

type Result = Unshift<[1, 2], 0>; // [0, 1, 2]

ํ’€์ด

ํฌ๊ฒŒ ์–ด๋ ค์šด ๋ฌธ์ œ๋Š” ์•„๋‹ˆ์—ˆ์Šต๋‹ˆ๋‹ค. ์ž…๋ ฅ๋ฐ›์€ U๋ฅผ ๋ฐฐ์—ด์˜ ๊ฐ€์žฅ ์•ž์ˆœ์„œ์— ๋ฐฐ์น˜ํ•˜์—ฌ ์ƒˆ๋กœ์šด array ํƒ€์ž…์„ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.

Union ํƒ€์ž…์˜ ์žฅ์ 

์‚ฌ์‹ค ์—ฌ๋‹ด์ด์ง€๋งŒ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๋ฐฐ์—ด๋กœ ์„ ์–ธํ•œ ํƒ€์ž…์€ ํ™œ์šฉ๋„๊ฐ€ ๋†’์ง€ ์•Š์Šต๋‹ˆ๋‹ค.
์œ ๋‹ˆ์–ธ ํƒ€์ž…์€ ๋ฐฐ์—ด๋ณด๋‹ค ๊ฐ„๋‹จํ•˜๊ณ  ๋ช…ํ™•ํ•ฉ๋‹ˆ๋‹ค. ํƒ€์ž…์„ ์ฝ์„ ๋•Œ, ๋ฐฐ์—ด๋ณด๋‹ค ์œ ๋‹ˆ์–ธ ํƒ€์ž…์ด ๋” ์ง๊ด€์ ์ด๊ณ  ์ดํ•ดํ•˜๊ธฐ ์‰ฝ์Šต๋‹ˆ๋‹ค.
์œ ๋‹ˆ์–ธ ํƒ€์ž…์€ TypeScript์˜ ํƒ€์ž… ์‹œ์Šคํ…œ๊ณผ ์ž์—ฐ์Šค๋Ÿฝ๊ฒŒ ํ†ตํ•ฉ๋ฉ๋‹ˆ๋‹ค.
์˜ˆ๋ฅผ ๋“ค์–ด, ์œ ๋‹ˆ์–ธ ํƒ€์ž…์€ ํ•จ์ˆ˜์˜ ๋งค๊ฐœ๋ณ€์ˆ˜๋‚˜ ๋ฐ˜ํ™˜ ๊ฐ’์˜ ํƒ€์ž…์œผ๋กœ ์‰ฝ๊ฒŒ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

Solution

/* _____________ ์—ฌ๊ธฐ์— ์ฝ”๋“œ ์ž…๋ ฅ _____________ */

type Unshift<T extends any[], U> = [U, ...T];

/* _____________ ํ…Œ์ŠคํŠธ ์ผ€์ด์Šค _____________ */
import type { Equal, Expect } from "@type-challenges/utils";

type cases = [
	Expect<Equal<Unshift<[], 1>, [1]>>,
	Expect<Equal<Unshift<[1, 2], 0>, [0, 1, 2]>>,
	Expect<Equal<Unshift<["1", 2, "3"], boolean>, [boolean, "1", 2, "3"]>>
];

Solution by adultlee #34115

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

Solution by ProvorovOleksii #34084

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

Solution by ouzexi #33974

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

Solution by hyojinLee-git #33947

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

Solution by notsecret32 #33865

type Unshift<T extends any[], U> = [U, ...T]

Solution by okasyun #33835

type Unshift<T extends any[], U> = [U, ...T]

pushใฎๅ›ž็ญ”ใ‚’ใปใผ้€†ใซใ—ใŸใฎใฟใ€‚ ้…ๅˆ—ใŒ็™ปๅ ดใ™ใ‚‹ๅ ดๅˆใฏใ ใ„ใŸใ„exntends type[]ใ‚’ๅฎฃ่จ€ใ—ใŸๆ–นใŒ่‰ฏใ•ใใ†ใ€‚ anyใฏใงใใ‚Œใฐไฝฟใ„ใŸใใชใ„ใ€‚ๅ•้กŒใซใ‚ˆใฃใฆใฏไฝฟใฃใฆใ„ใ‚‹ๅž‹ใŒ้™ใ‚‰ใ‚Œใฆใ„ใ‚‹ใฎใงใ€ใ‚‚ใฃใจไธๅฏงใซๅฎฃ่จ€ใ—ใฆใ‚‚่‰ฏใ„ใ‹ใ‚‚

Solution by okabe-yuya #33827

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

Solution by veralex #33758