-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Cannot assign generic type aliases that should be equivalent. #40312
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Tagging @DanielRosenwasser |
Honestly, I'm kind of surprised we never ran into this earlier. |
My guess would be that this changed after #29817. |
Piping in! I had a similar issue occurring in Typescript 3.9 and 4.1.3 (playground) and 4.2.0-dev (playground). I've got a type called GetKeysOfType that creates a union of all possible keys of an object whose types are the same as the provided type. This works perfectly as when I type something with it the values that I can assign to it are scoped correctly. type GetKeysOfType<O, T extends O[keyof O]> = { -readonly [P in keyof O]: O[P] extends T ? P : never }[keyof O];
class BDocument<T, I extends T[keyof T]> {
}
class Collection<T, I extends T[keyof T]> {
index!: GetKeysOfType<T, I>;
indexToDoc = new Map<I, BDocument<T, I>>();
async get() {
var val: T; //Presume that this value is retrieved from a remote and is correctly typed.
this.indexToDoc.get(val[this.index]); //This is where the error occurs.
}
} |
I am not sure if this is an instance of the same underlying bug, but I have run into a succinct typing problem which effects primitve types specifically: // specify type directly
declare let x: { [K in keyof string]: Promise<string[K]> };
x.length; // has type `Promise<number>`
// specify type using a type alias
type L<T> = { [K in keyof T]: Promise<T[K]> };
declare let y: L<string>;
y.length; // has type `number`
declare let z: L<{ length: number }>;
z.length; // has type `Promise<number>` Perhaps there is some limitation on generic type parameters that I am not aware of? If this is indeed intended behavior, it seems confusing. This was tested in the playground with 4.2.3 and 4.3.0-nightly. |
Linking in another repro interface A<T> {
field : T | null;
}
type assignTo = A<string[]>;
interface assignToExtended extends A<string[]> {}
function foo () : A<null> {
return {
field : null
};
}
// This consistently fails on all versions. (and I think this is intended)
const a : assignTo = foo();
// This used to work, but now starts to fail from 4.3.1-rc
const b : assignToExtended = foo(); |
Cannot assign generic type aliases that should be equivalent.
Simple test case:
Note that 'x' is not assignable to 'y' with
strictNullChecks
on. It results in the error:This seems very strange though.
Example
is just a type alias, which shouldn't have an impact on assignability. If we simply expand the type alias, we get thex1/y1
case. In the expanded form the compiler allows this just fine.Note: this was working in 3.3. It does not seem to work on 4.0. I'm not sure at what point it may have broken.
The text was updated successfully, but these errors were encountered: