You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
test.ts:7:3 - error TS2322: Type '{ x: boolean; } & Pick<A, Exclude<keyof A, "x">>' is not assignable to type 'A'.
'{ x: boolean; } & Pick<A, Exclude<keyof A, "x">>' is assignable to the constraint of type 'A', but 'A' could be instantiated with a different subtype of constraint 'AConstraint'.
It seems like this should work, since { x, ...rest } should be assignable to A regardless of how A is instantiated.
The error seems correct: you have a constraint on A that it's at least AConstraint so you can work with the value you have as though it's an AConstraint, but you can't actually produce a value of type A because you don't know how specific A actually is. There's no lower bound, so it could be any subtype at all.
For example:
functionfn<AextendsAnimal>(arg: A): A{returnnewGiraffe("Jeff");// Giraffe is assignable to Animal, but...}letcat=newCat("Mittens");letnewCat=fn(cat);// ...we just wanted a cat. :-(
As you point out, in your case you capture ...rest which should cover all the bases in practice--but I think TS is just assuming the worst: A might be never. Then nothing at all would be assignable to it.
TypeScript Version: 3.6.0-dev.20190619
Search Terms: type parameter generic constraint assignable instantiated subtype reconstruct
Code
Expected behavior:
pass
Actual behavior:
It seems like this should work, since
{ x, ...rest }
should be assignable toA
regardless of howA
is instantiated.Playground Link:
https://www.typescriptlang.org/play/#src=interface%20AConstraint%20%7B%0D%0A%20%20x%3A%20boolean%3B%0D%0A%7D%0D%0A%0D%0Afunction%20fn%3CA%20extends%20AConstraint%3E(arg%3A%20A)%3A%20A%20%7B%0D%0A%20%20const%20%7B%20x%2C%20...rest%20%7D%20%3D%20arg%3B%0D%0A%20%20return%20%7B%20x%2C%20...rest%20%7D%3B%0D%0A%7D%0D%0A
Related Issues:
#29049
The text was updated successfully, but these errors were encountered: