1 Star 1 Fork 0

wangzichu/ts-challenges

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
15.最后一个元素.ts 918 Bytes
一键复制 编辑 原始数据 按行查看 历史
WangZiChu199910252255 提交于 2022-11-04 11:22 . 提交
/*
15 - 最后一个元素
-------
by Anthony Fu (@antfu) #中等 #array
### 题目
> 由谷歌自动翻译,欢迎 PR 改进翻译质量。
>在此挑战中建议使用TypeScript 4.0
实现一个通用`Last<T>`,它接受一个数组`T`并返回其最后一个元素的类型。
例如
```ts
type arr1 = ['a', 'b', 'c']
type arr2 = [3, 2, 1]
type tail1 = Last<arr1> // expected to be 'c'
type tail2 = Last<arr2> // expected to be 1
```
> 在 Github 上查看:https://tsch.js.org/15/zh-CN
*/
/* _____________ 你的代码 _____________ */
type Last<T extends any[]> = T extends [...any[],infer R] ? R : never
type t = Last<[3]>
/* _____________ 测试用例 _____________ */
import type { Equal, Expect } from '@type-challenges/utils'
type cases = [
Expect<Equal<Last<[3, 2, 1]>, 1>>,
Expect<Equal<Last<[() => 123, { a: string }]>, { a: string }>>,
]
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/wang-zichu/ts-challenges.git
git@gitee.com:wang-zichu/ts-challenges.git
wang-zichu
ts-challenges
ts-challenges
master

搜索帮助