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
I believe this may just be a gap in how optional interface properties work under the hood, but it's possible to (using spread or assign) produce values that don't match the type systems expectations. You can see that in this example, but in short: Spread/assign are internally check [[GetOwnProperty]] to overwrite a value, so undefined properties can overwrite ones with concrete values. This results in values that don't represent the calculated Intersection.
I think this is caused by how optional properties are represent internally as (at times) T | undefined. Although this holds true for object lookup foo[prop], it does mean that we can create some incorrect values such as this.
const spread = {
foo: {},
...undefinedFoo
};
const assigned = Object.assign({}, {foo: {}}, undefinedFoo);
// TS has typed these as `number` but at runtime will produce "Cannot read property 'a' of undefined"
spread.foo['a'];
assigned.foo['a'];
The type system does not report any errors although the code is guaranteed to produce an error
🙂 Expected behavior
Type system should prevent an optional property from being assigned to undefined unless it was defined as | undefined or the Intersection should contain | undefined
The text was updated successfully, but these errors were encountered:
Ah, thanks for pointing in the direction @fatcerberus and @RyanCavanaugh . It looks like exactly the gap I believed I found was fixed! I tested against nightly but didn't think to recheck the options. I'll close this one.
Bug Report
I believe this may just be a gap in how optional interface properties work under the hood, but it's possible to (using spread or assign) produce values that don't match the type systems expectations. You can see that in this example, but in short: Spread/assign are internally check
[[GetOwnProperty]]
to overwrite a value, soundefined
properties can overwrite ones with concrete values. This results in values that don't represent the calculated Intersection.I think this is caused by how optional properties are represent internally as (at times)
T | undefined
. Although this holds true for object lookupfoo[prop]
, it does mean that we can create some incorrect values such as this.Resulting in errors like this
🔎 Search Terms
interface types, optional properties, intersection, spread, assign
🕗 Version & Regression Information
This is the behavior in every version I tried, and I reviewed the FAQ for entries about optional properties and spread/assign
⏯ Playground Link
Playground link with relevant code
💻 Code
(see in description)
🙁 Actual behavior
The type system does not report any errors although the code is guaranteed to produce an error
🙂 Expected behavior
Type system should prevent an optional property from being assigned to
undefined
unless it was defined as| undefined
or the Intersection should contain| undefined
The text was updated successfully, but these errors were encountered: