Giter Site home page Giter Site logo

Comments (11)

semlinker avatar semlinker commented on May 18, 2024 17

8.1 解法一

type NonEmptyArray<T> = [T, ...T[]]

8.2 解法二

type NonEmptyArray<T> = T[] & { 0: T };

from awesome-typescript.

zhaoxiongfei avatar zhaoxiongfei commented on May 18, 2024 4
// 定义 NonEmptyArray 工具类型,用于确保数据非空数组。

type NonEmptyArray<T> = [T, ...T[]];

const a: NonEmptyArray<string> = [] // 将出现编译错误
const b: NonEmptyArray<string> = ['Hello TS'] // 非空数据,正常使

[T, ...T[]] 确保了第一项一定是T, 代码简洁的同时还易于理解

from awesome-typescript.

sunboyZgz avatar sunboyZgz commented on May 18, 2024 3
//貌似也能用
type NonEmptyArray<T> = T[]["length"] extends 0 ? never : T[];

from awesome-typescript.

winfa avatar winfa commented on May 18, 2024 2
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.

Mrlgm avatar Mrlgm commented on May 18, 2024
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.

waleiwalei avatar waleiwalei commented on May 18, 2024

// 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.

YJCCreateAHistory avatar YJCCreateAHistory commented on May 18, 2024
// 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.

Bo-Teng avatar Bo-Teng commented on May 18, 2024

@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.

guo897654050 avatar guo897654050 commented on May 18, 2024

8.1 解法一

type NonEmptyArray<T> = [T, ...T[]]

8.2 解法二

type NonEmptyArray<T> = T[] & { 0: T };

这个& {0: T}啥意思啊,代表T[0]的类型是T类型?可以这么写吗

from awesome-typescript.

ZHAOLIMIN1997 avatar ZHAOLIMIN1997 commented on May 18, 2024
// 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.

shiyicyh avatar shiyicyh commented on May 18, 2024
//<数组类型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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.