You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Expected behavior:
function declaration is allowed Actual behavior:
function declaration errors, saying that T cannot be used to index Obj2, even if it should be allowed.
There's a workaround for this by declaring the function like this: function func<T extends Keys>(p: T extends keyof Obj ? Obj[T] : T extends keyof Obj2 ? Obj2[T] : never);
However this workaround causes the bug of scenario 2.
Scenario 2
Code
type TestType = "a" | "b";
type Obj = {
a: string;
b: number;
};
function func<T extends TestType>(value: T extends TestType ? Obj[T]: never) { };
function func2<T extends TestType>(value: T extends "a" ? string : number) { };
interface Prop1 {
update<T extends TestType>(value: T extends "a" ? string : number) : void;
}
const x: Prop1 = { update: func }; //doesn't work
const y: Prop1 = { update: func2 }; //works
Actual behavior:
the line were x is declared is giving an error that func is not assignable to the update function of Prop1, even if it should.
Expected behavior:
x should be allowed to be defined.
I know that the type of func is a bit stupid (the conditional type is not necessary here), but in my use case a similar type comes from the problem in scenario 1, forcing me to type it that way.
The problem is in the ": never" part of the conditional type, as another conditional type that doesn't have that :never part seems to work.
The text was updated successfully, but these errors were encountered:
Generic types do not behave any differently if they are constrained to unit types. in general the compiler can not make assumptions about the generic type possible values being limited to one set or another in the presence of intersection types.
For the second example, comparing conditional types really assumes they have to be identical. We are looking into updating some of these relationships, but so far we can not conclusively prove or disprove additional higher order relationships, see #24940 for related discussions.
Thank you for your answer. I understand that both scenarios are quite complicated and I expected it to be a current design limitation, but you guys have been made so many great advancements in what the compiler can actually do that I hope that sooner or later something similar will be possible
TypeScript Version: 2.9.2
Found two bugs while creating some conditional types using mapped types
Scenario 1
Code
Scenario 1
Expected behavior:
function declaration is allowed
Actual behavior:
function declaration errors, saying that T cannot be used to index Obj2, even if it should be allowed.
There's a workaround for this by declaring the function like this:
function func<T extends Keys>(p: T extends keyof Obj ? Obj[T] : T extends keyof Obj2 ? Obj2[T] : never);
However this workaround causes the bug of scenario 2.
Scenario 2
Code
Actual behavior:
the line were x is declared is giving an error that func is not assignable to the update function of Prop1, even if it should.
Expected behavior:
x should be allowed to be defined.
I know that the type of func is a bit stupid (the conditional type is not necessary here), but in my use case a similar type comes from the problem in scenario 1, forcing me to type it that way.
The problem is in the ": never" part of the conditional type, as another conditional type that doesn't have that :never part seems to work.
The text was updated successfully, but these errors were encountered: