-
Notifications
You must be signed in to change notification settings - Fork 12.6k
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
Generic type inferance not working #60981
Comments
It seems that the generic type was inferred from the second argument class DialogService {
open = <DialogData>(
component: ComponentType<TypedDialog<DialogData>>,
- config: DialogData
+ config: NoInfer<DialogData>
) => {};
} |
This is basically the same behavior as seen in e.g. interface Foo {
s: number;
}
interface FooNull {
s: number | undefined;
}
function f<T>(arg1: T, arg2: T) {
}
f({} as Foo, {} as FooNull); There's no error because aliased property writes in TS are generally unsound and the implied initialization of |
@whzx5byb thanks for mentioning @RyanCavanaugh Sorry but I still don't quite understand the situation. But in my example the type is not widened but rather is TypeScript lying to me. It is not inferring the type of the wrong argument. class DialogService {
open = <DialogData>(
component: ComponentType<TypedDialog<DialogData>>,
config: DialogData
) => {};
}
abstract class TypedDialog<DialogData> {
dialogData!: DialogData;
}
class Component extends TypedDialog<{
area: Date;
// test: string; // uncomment to get error about area being nullable
}> {
area: string = this.dialogData.area.toISOString(); // <-- runtime error
}
const service = new DialogService();
let data: Date | null = null;
if (2 as any == "cantMatch") {
data = new Date();
}
service.open(Component, {
area: data,
});
declare interface ComponentType<T> {
new (...args: any[]): T;
} This example throws no compile error but fails hard at runtime. |
π Search Terms
"generic type inferance"
π Version & Regression Information
β― Playground Link
https://www.typescriptlang.org/play/?jsx=0#code/MYGwhgzhAEAiCWYQHsDmBlApgJwG72E2gG8AoaC6ZAB0wDtoBeaAHgSTVjABcwA+ABTlKI4MgC21ZHXrcAXNADCEqTLrcAKgE9aLbbQAm7FKjaITXXnz4AaYSIpi6AM3ioFxzjzD3oASiY+EgBfAG5SYNJSMAAjCG5sMGBuaFBIGH1MI3M0Mw5US34SXwMcgu8AQg8ywvDI0jSoJRVpWWhMAA9uegMMnSzPUzIRMGxMMAU6AFdxGJxwygB6Rehu+IV47Hg6VFDoZegpujFxcTbuZGhUTBScbGRsaFjkKZTR8eg57dRoaZBwGIgTARILDSjvCa-GZzR7MbgAC3gEAAdKV8oVkRC6lEnPFoBAcPhCExfpgAO5wMpYPAETACPzhBrSPEGbwkgBMTxg01mOGgAB8of9GQSaYRkTR6AJlJJWuobMURmNIazeAqDsAptgxuoQFpfpc7g8FRB4S8QAZPkQAAaZaAAch5MIFQpA9ugSINbygbjosSBq0u3H6DqdOHtyOtEQZUQMmDSYw96hwziSRBlqlkmT0oN8MgpAmRRdGqAgCjAdC0AG0ALp+BQaOpAA
π» Code
π Actual behavior
when
test
is not there, no error about type mismatch ofarea: data
π Expected behavior
type error at any time
Additional information about the issue
this seems like a bug as adding new properties show an already existing error.
but maybe I am just misunderstanding how typescript generics work?
The text was updated successfully, but these errors were encountered: