This example doesn't typecheck:
type Subset1 = "one" | "two";
type Subset2 = "three" | "four";
type All = Subset1 | Subset2;
type Other = {
"one": number,
"two": string,
"three": boolean,
"four": object,
};
type Extra<V> = V extends Subset1 ? string : undefined;
function doOtherThing(stuff: string){}
function doThing<V extends All>(value: V, params: Other[V], extra: Extra<V>) { }
function doSubset1Thing<V extends Subset1>(value: V, params: Other[V], extra: string) {
doThing(value, params, extra);
doOtherThing(extra);
}
function doSubset2Thing<V extends Subset2>(value: V, params: Other[V]) {
doThing(value, params, undefined);
}
The error is because extra
is hardcoded string
in doSubset1Thing
, but logically it is always a string because value
is limited to Subset1
, and Extra<Subset1>
properly resolves to string
, but for some reason, the call to doThing
does not recognize that.
Similarly, inverting it for doSubset2Thing
errors even though the third param will always be undefined
.
For the second one, I can sort of see issues if Subset1
and Subset2
overlapped, but they don't, so I'd have assumed that TS would flatted that all out to undefined
for doSubset2Thing
.
Is there any way to make this work? Alternatively, am I missing something that actually does make this invalid?