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
// Returns keyes that have type extending Types
type KeysMatching<T, Types> = { [K in keyof T]: T[K] extends Types ? K : never }[keyof T];
type Mapping = {
STR: string;
NUM: number;
BOOL: boolean;
}
type Values = Mapping[keyof Mapping];
type Generic<T extends Values> = {
typeStr: KeysMatching<Mapping, T>
}
const foo: Generic<boolean> = { typeStr: 'BOOL' };
const bar: Generic<Values> = foo;
// Type 'Generic<boolean>' is not assignable to type 'Generic<Values>'.
// Type 'Values' is not assignable to type 'boolean'.
// Type 'string' is not assignable to type 'boolean'.(2322)
// This works fine
type Conditional<T, V> = T extends V ? T : unknown;
const foo2: Conditional<string, Values> = 'string';
const bar2: Conditional<Values, Values> = foo2;
🙁 Actual behavior
TS throws error that foo is not assignable to bar
🙂 Expected behavior
TS does not throw error as the types should be compatible
The text was updated successfully, but these errors were encountered:
Probably something to do with variance measurement - it seems like V<T> is incorrectly being measured as contravariant/invariant in T, likely because T occurs on the RHS of an extends check. R2 is correctly determined to be true because V0 extends VNumber bypasses variance measurement and does a full structural check.
Bug Report
Cannot assign type which should be compatible when conditional generics are used (also posted on SO but was only suggested to open issue here)
🕗 Version & Regression Information
Throws error on all versions available in TypeScript playground.
Playground Link
⏯ Playground Link
💻 Code
🙁 Actual behavior
TS throws error that foo is not assignable to bar
🙂 Expected behavior
TS does not throw error as the types should be compatible
The text was updated successfully, but these errors were encountered: