Skip to content

Commit

Permalink
add: 00014-easy-first
Browse files Browse the repository at this point in the history
  • Loading branch information
yuexiaoliang committed Jan 28, 2023
1 parent 043ec2b commit 9473588
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions 00014-easy-first.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// ============= Test Cases =============
import type { Equal, Expect } from './test-utils';

type cases = [Expect<Equal<First<[3, 2, 1]>, 3>>, Expect<Equal<First<[() => 123, { a: string }]>, () => 123>>, Expect<Equal<First<[]>, never>>, Expect<Equal<First<[undefined]>, undefined>>];

type errors = [
// @ts-expect-error
First<'notArray'>,
// @ts-expect-error
First<{ 0: 'arrayLike' }>
];

// ============= Your Code Here =============

// 判断是否为空数组
// type First<T extends any[]> = T extends [] ? never : T[0];

// 判断长度是否为 0
// type First<T extends any[]> = T['length'] extends 0 ? never : T[0];

// 使用 infer 取第零个元素
type First<T extends any[]> = T extends [infer F, ...any[]] ? F : never;

0 comments on commit 9473588

Please sign in to comment.