Comments (11)
8.1 解法一
type NonEmptyArray<T> = [T, ...T[]]
8.2 解法二
type NonEmptyArray<T> = T[] & { 0: T };
from awesome-typescript.
// 定义 NonEmptyArray 工具类型,用于确保数据非空数组。
type NonEmptyArray<T> = [T, ...T[]];
const a: NonEmptyArray<string> = [] // 将出现编译错误
const b: NonEmptyArray<string> = ['Hello TS'] // 非空数据,正常使
[T, ...T[]] 确保了第一项一定是T, 代码简洁的同时还易于理解
from awesome-typescript.
//貌似也能用
type NonEmptyArray<T> = T[]["length"] extends 0 ? never : T[];
from awesome-typescript.
type NonEmptyArray<T> = // 你的实现代码 const a: NonEmptyArray<string> = [] // 将出现编译错误 const b: NonEmptyArray<string> = ['Hello TS'] // 非空数据,正常使用
type NonEmptyArray<T> = {
[P in number]: T;
} & {
0: T
};
from awesome-typescript.
type NonEmptyArray<T> = {
[P in (keyof T[] & 0)]: P extends number ? T : T[][P]
}
// 测试用例
const a: NonEmptyArray<string> = [] // 将出现编译错误
const b: NonEmptyArray<string> = ['Hello TS'] // 非空数据,正常使用
from awesome-typescript.
// 22.3.11 - Learn
// 解法1
type NonEmptyArray = {
0: T
}
// 解法2
type NonEmptyArray = [T, ...T[]]
const a: NonEmptyArray = [] // 将出现编译错误
const b: NonEmptyArray = ['Hello TS'] // 非空数据,正常使用
from awesome-typescript.
// type NonEmptyArray<T> = [T,...T[]]// 你的实现代码
type NonEmptyArray<T> = {
[P in number] : T
} & {0 : T}
const a: NonEmptyArray<string> = [] // 将出现编译错误
const b: NonEmptyArray<string> = ['Hello TS']// 非空数据,正常使用
from awesome-typescript.
@zhaoxiongfei
在你的基础上补充了下:
type NonEmptyArray = {
[P in (keyof T[] & 0)]: P extends number ? T | undefined : T[][P]
}
const a: NonEmptyArray = [] // 将出现编译错误
const b: NonEmptyArray = ['Hello TS', 'Hello TS'] // 非空数据,正常使用
const C: NonEmptyArray = [,1,,1,] // 含空数组
from awesome-typescript.
8.1 解法一
type NonEmptyArray<T> = [T, ...T[]]8.2 解法二
type NonEmptyArray<T> = T[] & { 0: T };
这个& {0: T}啥意思啊,代表T[0]的类型是T类型?可以这么写吗
from awesome-typescript.
// type NonEmptyArray<T> = [T,...T[]]// 你的实现代码 type NonEmptyArray<T> = { [P in number] : T } & {0 : T} const a: NonEmptyArray<string> = [] // 将出现编译错误 const b: NonEmptyArray<string> = ['Hello TS']// 非空数据,正常使用
这样写键是number类型的对象也是可以的
from awesome-typescript.
//<数组类型T>,T[]定义内容需要是数组,{0:T}定义第一项数据类型为T以此保证不为空
type NonEmptyArray<T> = T[] & {0:T};
//元祖定义,定义第一项必须存在且类型为T;...T[]为元祖的剩余元素定义,定义可有任意个T类型元素
type NonEmptyArray<T> = [T,...T[]]
//const a: NonEmptyArray<string> = []// 出现编译错误
const b: NonEmptyArray<string> = ['Hello TS'] // 非空数据,正常使用
from awesome-typescript.
Related Issues (20)
- typescript练习第二题
- 报错的原因是返回值类型不匹配,返回值的类型是User,但T比User的范围更大
- ...
- #### 7.1 EmptyObject
- 「重学TS 2.0 」TS 练习题第四十一题 HOT 9
- 「重学TS 2.0 」TS 练习题第四十二题 HOT 12
- 「重学TS 2.0 」TS 练习题第四十三题 HOT 10
- 「重学TS 2.0 」TS 练习题第四十四题 HOT 4
- 「重学TS 2.0 」TS 练习题第四十五题 HOT 4
- 「重学TS 2.0 」TS 练习题第四十六题 HOT 5
- 「重学TS 2.0 」TS 练习题第四十七题 HOT 7
- 「重学TS 2.0 」TS 练习题第四十八题 HOT 4
- readme里的电子书下载地址403了 HOT 2
- 疑惑返回类型加Partial为什么不行 HOT 1
- 第一题
- 收藏学习!!!
- 抱歉,点击错了
- 「重学TS 2.0 」TS 练习题第四十九题 HOT 4
- > ```ts
- 练习第一题 HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from awesome-typescript.