Skip to content

Partial does not work inside a method that uses the class generic parameter type #14265

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

Closed
asfernandes opened this issue Feb 23, 2017 · 3 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@asfernandes
Copy link

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 to Partial<T> as 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
@mhegazy
Copy link
Contributor

mhegazy commented Feb 23, 2017

The error is correct since T can be more specialized than its constraint {id: number}. consider:

const component = new Component<{id: 2}>(intf);
component.intf.find({ id: 1 });	// Error

@mhegazy mhegazy added the Working as Intended The behavior described is the intended behavior; this is not a bug label Feb 23, 2017
@asfernandes
Copy link
Author

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.

@mhegazy
Copy link
Contributor

mhegazy commented Feb 23, 2017

That is a duplicate of #14150

@mhegazy mhegazy closed this as completed Apr 21, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

2 participants