#How can I map a tuple of objects to a tuple with optional elements?
48 messages ยท Page 1 of 1 (latest)
The optionality can only be applied to object attributes, not simple types
That bring said, you can very well iterate through that tuple type, using mapped types
And using conditional types, extract type you want, and apply an union with undefined if needed
!hb mapped typed
!hb conditional types
What?
number is a type.
The syntax [string, number?] is a 2-element tuple with an optional 2nd element.
I'm sorry, I thought people knew TypeScript here. My mistake.
Please just... look it up. try it in an IDE. whatever
type T = number?;
// ^^^^^^^
// '?' at the end of a type is not valid TypeScript syntax. Did you mean to write 'number | undefined'?
That's not the syntax I used.
that's because tuples are fixed length arrays
and arrays are technically objects
and object can have optional properties
but I wouldn't recomment using that notation
no, it's a tuple with optional elements
you have no idea what you're talking about
it's just a thing about the language you don't know
it's not even new
it's almost never used
because it goes against what tuples are for
the points of tuples is to have a well known length
how old are you?
but if you don't know the exact length at compile time, and still need to perform a check, then it's not that different from a regular array
this is not a question about language design or your lack of knowledge about the language
if you don't know the answer don't say random stuff. just don't say
wdym? I pointed you to the corresponding section in the handbook
even writing a playground code example atm with the solution
and even suggest you not to use that notation because it's not well known and goes against the very essence of tuples
but "if you don't know the answer don't say random stuff. just don't say"
does it end up with the type [X, y?]?
I have a feeling not
there are no many features about the language that are not well known and go against the very essence of everything
the language is deeply unsound
this is a waste of time
type Item<O extends boolean = boolean, T = unknown> = {
optional: O;
element: T;
};
type ExampleInput = [Item<false, string>, Item<true, number>];
type ReduceItems<A extends [Item, ...Item[]]> = {
[K in keyof A]: A[K] extends Item<true, infer T> ? T | undefined : A[K] extends Item<false, infer T> ? T : never;
}
type Foo = ReduceItems<[{ optional: true, element: "element" }]>;
// ^?
!ts
type Item<O extends boolean = boolean, T = unknown> = {
optional: O;
element: T;
};
type ExampleInput = [Item<false, string>, Item<true, number>];
type ReduceItems<A extends [Item, ...Item[]]> = {
[K in keyof A]: A[K] extends Item<true, infer T> ? T | undefined : A[K] extends Item<false, infer T> ? T : never;
}
type Foo = ReduceItems<[{ optional: true, element: "element" }]>;
// ^? - type Foo = ["element" | undefined]
Using mapping modifiers would require you to create a recursive type
and append the elements one by one, making them optional or not every time
merging the result every time using intersection
๐ but there is also a risk the result is also incorrect, since "optional tuple elements" must come last
and you could very well declare some items as optional at the very front of the tuple
I'm not aware of any way to create [T?] via type manipulation, however you can do [] | [T].
that will result in an explosion of possibilities ๐ค
Oh they aren't even in the thread anymore.
At least it's just O(n) so it probably won't be that bad.
But yeah you do have to deal with enforcing all optional elements must be at the end, or I guess you could be fault tolerant and treat [A?, B] the same as [] | [A, B]/[B] | [A, B].