gpt4 book ai didi

typescript :如何正确键入由 n 个 N 元组和 m 个 M 元组组成的数组?

转载 作者:行者123 更新时间:2023-12-03 16:33:20 26 4
gpt4 key购买 nike

//An N-tuple (quadruple?) that is a subset of a row
type Quadruple = [string, string, number, boolean];

//A value that is a subset of a row
type Value = [string];

//A row in the format [...Quadruple[], ...Value[]]
type Row = (string | boolean | number)[]; //this works...but information is lost.

const egRow1: Row = ["id", "name", 1, true, "id2", "name2", 1, true, "id3", "name3", 1, false, "value1", "value2"];
const egRow2: Row = ["id", "name", 1, true, "id2", "name2", 1, true, "value1", "value2"];
const egRow3: Row = ["id", "name", 1, true, "value1", "value2"];
如何有效地输入一行来编码它由 M Quadruples 组成的事实其次是 N Values ?

最佳答案

在我看来,这只能通过枚举 Quadruple 的所有可能数量 (M) 来完成。并联合这些类型:

type Row1 = [string, string, number, boolean, ...Array<string>];
type Row2 = [string, string, number, boolean, string, string, number, boolean, ...Array<string>];
type Row3 = [string, string, number, boolean, string, string, number, boolean, string, string, number, boolean, ...Array<string>];
...
type RowM = [string, string, number, boolean, ..., ...Array<string>];

type Row = Row1 | Row2 | Row3 | ... | RowM;
Playground

关于 typescript :如何正确键入由 n 个 N 元组和 m 个 M 元组组成的数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63149464/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com