Repository navigation
Replies: 1 comment
|
@marijn Hi! thanks :)
Yeah, I'd really like to support this. Unfortunately, it doesn't appear TypeScript supports programmatic element labeling (yet) ... but it might be possible in future. To support, would need TypeScript to allow the following (where the // Make Labelled Element
type Label<Name extends string, Value extends number, Result extends [number] = [Name: Value]> = Result
// ^ ^
// Unused Argument Label (Not Generic Argument)
// Assign Labels to Values
type Labels<Values extends number[], Result extends unknown[] = []> = (
Values extends [infer L extends number, ...infer R extends number[]]
? Labels<R, [...Result, ...Label<`Foo${Result['length']}`, L>]>
: Result
)
// Example
type A = [1, 2, 3]
type B = Labels<A> // actual: [Name: 1, Name: 2, Name: 3]
// expect: [Foo0: 1, Foo1: 2, Foo3: 3]TypeBox has made some provisions for this feature, and element labeling is technically supported with Script (...) today, but sadly needs to infer without labels for now, so just waiting on TypeScript to provide language support to go further. import Type from 'typebox'
// Tip: Use TS7 for Unicode ° inference
export type GeoCoordinate = Type.Static<typeof GeoCoordinate> // [`${number}°N`, `${string}°E`]
export type Lat = Type.Static<typeof Lat> // `${number}°N`
export type Long = Type.Static<typeof Long> // `${string}°E`
const { Lat, Long, GeoCoordinate } = Type.Script(`
export type Lat = \`\${number}°N\`;
export type Long = \`\${string}°E\`;
export type GeoCoordinate = [
lat: Lat,
long: Long
];
`)Hope this brings some insight! |
Uh oh!
There was an error while loading. Please reload this page.
First things first: Thanks for Typebox, you have made something useful and interoperable 🫶
Named tuple values are available in typescript native tuple syntax
As far as I can tell this is not available with Typebox, but asking never hurts.
For now this type is one that I've retained hard-coded over deriving it from the schema, because I find it helps with he developer experience of the people that have to interact with my tuple.
All reactions