We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
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
TypeScript Version: 2.1.6
The subject is a bit difficult to write, but look in the code inside method.
method
{ id: 1 } should be implicit convertible to Partial<T> as T extends { id: number }.
{ id: 1 }
Partial<T>
T extends { id: number }
interface Intf<T extends { id: number }> { find: (obj: Partial<T>) => T; } interface Obj { id: number; name: string; } const intf: Intf<Obj> = { find(obj: Obj) { return obj; } } intf.find({ id: 1, name: '' }); // ok intf.find({ id: 1 }); // ok class Component<T extends { id: number }> { constructor (public intf: Intf<T>) { } method() { this.intf.find({ id: 1 } as T); // ok this.intf.find({ id: 1 }); // error! why? } } const component = new Component<Obj>(intf); component.intf.find({ id: 1, name: '' }); // ok component.intf.find({ id: 1 }); // ok
The text was updated successfully, but these errors were encountered:
The error is correct since T can be more specialized than its constraint {id: number}. consider:
T
{id: number}
const component = new Component<{id: 2}>(intf); component.intf.find({ id: 1 }); // Error
Sorry, something went wrong.
Then what about this?
class Component<T extends { id: number }> { constructor (obj: T) { obj.id = 10; } } new Component<{id: 2}>({ id: 2 });
It works and violates the type system as well the behavior I intended does.
That is a duplicate of #14150
No branches or pull requests
TypeScript Version: 2.1.6
The subject is a bit difficult to write, but look in the code inside
method
.{ id: 1 }
should be implicit convertible toPartial<T>
asT extends { id: number }
.The text was updated successfully, but these errors were encountered: