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
typeA={a:number,b:number}consta={a:1,c:false}constb:Partial<A>={a:2}constc:Partial<A>={...a}// doesn't catch field "c" being not specified on type Aconstd:Partial<A>={...a,c:3}// prevents specifying field not from type A
π Actual behavior
Object that partially matches type structure was allowed to be destructured into object with defined type, despite having a field that not specified on mentioned type.
Meanwhile when defining not specified field directly on object Typescript will throw an error "Object literal may only specify known properties"
π Expected behavior
Since object has a not specified property from type, it should throw an error, same as defining a not specified field directly on object
Both destructuring an defining not specified property lead to object have property that doesn't match type structure.
Not catching error when destructuring can easily lead to incorrect behavior, as an example, using function with a lot of parameters that meant to be passed to next function, and describing new property that doesn't exist on type of next function parameters leads function not accounting for this property or even processing it when it shouldn't.
Additional information about the issue
No response
The text was updated successfully, but these errors were encountered:
π Search Terms
Destructuring object, not specified fields, not caught
π Version & Regression Information
This is the behavior in every version I tried, and I reviewed the FAQ for entries about Common "Bugs" That Aren't Bugs
β― Playground Link
https://www.typescriptlang.org/play/?#code/C4TwDgpgBAglC8UDeBDAXAOwK4FsBGEATgDRR6a4GEC+AULQMYD2GAzsFCgsugIykM0AMxQAbVhGpRGLdmTQAFFIWABLMQB4YAPm6o0AJjrM2HQUpXrRW3YiQA6RymOyOAE0XK1mnXsf2UYkEAZjogA
π» Code
π Actual behavior
Object that partially matches type structure was allowed to be destructured into object with defined type, despite having a field that not specified on mentioned type.
Meanwhile when defining not specified field directly on object Typescript will throw an error "Object literal may only specify known properties"
π Expected behavior
Since object has a not specified property from type, it should throw an error, same as defining a not specified field directly on object
Both destructuring an defining not specified property lead to object have property that doesn't match type structure.
Not catching error when destructuring can easily lead to incorrect behavior, as an example, using function with a lot of parameters that meant to be passed to next function, and describing new property that doesn't exist on type of next function parameters leads function not accounting for this property or even processing it when it shouldn't.
Additional information about the issue
No response
The text was updated successfully, but these errors were encountered: