-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Type constraint is not respected when using nested generic types B<A> where A is a property type of B #27427
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
Smaller repro: type Test<T extends string = T> = { value: T } // Why is this allowed?
let zz: Test = { foo: "abc" }; // Type is Test<{}> ?!? Something is not right here. We're allowing a type parameter to be its own default value (I'm not sure what that even means), but we seem to completely ignore the constraint when the type parameter isn't specified. |
The assignment also works without the default (I only left the default in my example above to show that TypeScript actually inferred the type 'unknown' instead of 'number'): export interface DocumentRef<T extends string> {
type: T;
}
export type PartialDocument<R extends DocumentRef<R['type']>> = {
[P in keyof R]?: R[P];
}
const test: PartialDocument<{type: number}>; // Works, but should give error: type 'number' is not assignable to type 'string' However, if you add the extends constraint of the DocumentRef generic as an intersection type to the property declaration bearing the generic type, it works as I would expect: export interface DocumentRef<T extends string> {
type: T & string; // <-- note the intersection type
}
export type PartialDocument<R extends DocumentRef<R['type']>> = {
[P in keyof R]?: R[P];
}
const test: PartialDocument<{type: number}>; // Error: type 'number' is not assignable to type 'string' |
@ahejlsberg your issue is different than @timargra 's, for sure. I have a fix ready for yours (its small - #28222), however the root cause' of @timargra 's is hard to fix. When we check |
Looks like this got fixed sometime, not sure where or when |
TypeScript Version: 3.2.0-dev.20180927
Code
Expected behavior:
Actual behavior:
The text was updated successfully, but these errors were encountered: