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
interfaceA{a: stringb: number}functiontest1(data: A): A{return{
...data,b: 'five'// correctly errors: Type 'string' is not assignable to type 'number'.}}functiontest2<TextendsA>(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.
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.
The text was updated successfully, but these errors were encountered:
This is still #10727; TS doesn't have a native spread type operator. The closest thing it has is intersection, which is simple and often good enough... except when you overwrite properties with incompatible types.
π 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
https://www.typescriptlang.org/play/?#code/JYOwLgpgTgZghgYwgAgILIN4Chm+XALmQGcwpQBzHPAIyJAFcBbG6LAXyyxgZATGAB7EMkikAjAAoAJnDCE0ASiLpseZFAhgGUEWvV4AdMdnyANNQPI6yAOQxgANwi3kAejfIEgqJv4AbAE9kaCgfYiIAFUCABxRbUnIQCldgYmQQQTB8YmJgChA4Gn8UMEFRWPjGFmhbQ0tkTk5uXn4hETEwACYAHkiQgA9IEGl01AA+GTkFSOU0TAbNbV0Fq1xjQ1M4CzXrInsnF3dPaUEIdMzs0J8zawZs4gALQQZ-aUMNLR0QdNBHOH8wGkyEENAAVhB+A0mlggA
π» Code
π Actual behavior
test2
function compiles without error, but when called at runtime, will always return an invalid object, with propertyb
of typestring
instead ofnumber
π Expected behavior
test2
function has a compiler error preventing spreading of theb
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.The text was updated successfully, but these errors were encountered: