Skip to content
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

Generic types wrapped with Partial cannot be assigned to empty objects #13908

Closed
JustASquid opened this issue Feb 6, 2017 · 2 comments
Closed
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@JustASquid
Copy link

TypeScript Version: 2.1.4

Code

interface I {
	a: string;
}

function f1<T extends Partial<I>>() {
	// Error TS2322
	let i: T = {};
}

function f2<T extends I>() {
	// Error TS2322
	let i: Partial<T> = {};
}

Expected behavior:
No errors

Actual behavior:
Error TS2322: Type '{}' is not assignable to type 'T' / 'Partial'

Forgive me if this is simply a limitation of the Typescript engine and the implementation of Partial, but I don't know enough about the internals to make a judgement there.

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Feb 6, 2017
@RyanCavanaugh
Copy link
Member

The first example is not a bug. Being constrained by a partial type does not mean that you don't have additional required properties. Remember that a constraint is an upper bound on a type, not a lower bound - the concrete type of T at any call site may have additional properties and those properties may be required; the Partial<I> constraint only means that any properties in T which match those of I must have types which are assignable to the corresponding types in I

The second example is fixed in master.

@JustASquid
Copy link
Author

Thanks Ryan, you're totally right about the first case.

@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