Description
π Search Terms
spread generic
spread generic unsafe
spread constrained generic invalid
π Version & Regression Information
This is the behavior in every version I tried, and I reviewed the FAQ for entries about "generics" and "spread"
β― Playground Link
π» Code
interface A {
a: string
b: number
}
function test1(data: A): A {
return {
...data,
b: 'five' // correctly errors: Type 'string' is not assignable to type 'number'.
}
}
function test2<T extends A>(data: T): A {
return {
...data,
b: 'five' // does not error, but should. returns invalid object
}
}
π Actual behavior
test2
function compiles without error, but when called at runtime, will always return an invalid object, with property b
of type string
instead of number
π Expected behavior
test2
function has a compiler error preventing spreading of the b
property with an invalid value.
Additional information about the issue
I found a similar issue, #60623
However, this issue has a more complex case, with an example function returning a generic. In my example, test2
declares a static return type, and unambiguously returns an invalid value that does not satisfy that type.