Skip to content

Partial<> with generics is throwing: Type 'xxx' is not assignable to type 'Partial<T>'.Β #63058

@jozefchutka

Description

@jozefchutka

πŸ”Ž Search Terms

partial, Type is not assignable to type 'Partial'.

πŸ•— Version & Regression Information

  • This is the behavior in every version I tried

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.9.3#code/C4TwDgpgBAKglgWwgEygXigbwFAEhiIQBcAdgK4IBGEATgNzYC+22AZmSQMYED2JUAcwjAACgEMaBMQBsAPDCgQAHsAglkAZ1iFkAPgAUASiLjJcGfN1Y8NYWRr9MBJEQAMzXAHpPscNADkmFDOxFCudFCM-lBwWiQ8wFBiGhpwAiRilNLQwDzBflD+plJyMLr+AHRMLOxcvPxCohIlAEzyiipqmtpIekYmzealVji4nHwaibYaZNLAA2YWZehYjAy407PAFSEr4TZ2DlCbc+veUADyANJMQA

πŸ’» Code

type Timed = {
	time:number;
}

function getPartial<T extends Timed>():Partial<T> {
	return {time:0}
	// Type '{ time: 0; }' is not assignable to type 'Partial<T>'.
}

function getPartial2<T extends Timed>():Partial<T> {
	const result:Partial<T> = {};
	result.time = 0;
	return result;
	// OK
}

πŸ™ Actual behavior

compiler throws Type '{ time: 0; }' is not assignable to type 'Partial<T>' for getPartial() while it is happy to create object with the same data via getPartial2()

πŸ™‚ Expected behavior

I would expect:

  • a more descriptive error
  • getPartial() should succeed or at least same behavior for getPartial() and getPartial2()

Additional information about the issue

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Not a DefectThis behavior is one of several equally-correct options

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions