Skip to content

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

Closed
@asfernandes

Description

@asfernandes

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions