#How can I map a tuple of objects to a tuple with optional elements?

48 messages ยท Page 1 of 1 (latest)

small jasper
#

number isn't an actual type

#

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

vast axleBOT
small jasper
#

!hb conditional types

vast axleBOT
lyric pivot
#

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.

small jasper
#

number? doesn't make sense

#

number exists
but number? doesn't

lyric pivot
#

Please just... look it up. try it in an IDE. whatever

small jasper
#

you can have { foo?: number }, but can't have number?

#
type T = number?;
#

!ts

vast axleBOT
#
type T = number?;
//       ^^^^^^^
// '?' at the end of a type is not valid TypeScript syntax. Did you mean to write 'number | undefined'?
lyric pivot
#

That's not the syntax I used.

small jasper
#

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

lyric pivot
#

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

small jasper
#

it's almost never used

#

because it goes against what tuples are for

#

the points of tuples is to have a well known length

lyric pivot
#

how old are you?

small jasper
#

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

lyric pivot
#

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

small jasper
#

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"

lyric pivot
#

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

small jasper
#
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

vast axleBOT
#
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]
small jasper
#

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

sturdy oracle
#

I'm not aware of any way to create [T?] via type manipulation, however you can do [] | [T].

small jasper
#

that will result in an explosion of possibilities ๐Ÿค”

sturdy oracle
#

Oh they aren't even in the thread anymore.

sturdy oracle
#

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