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
{{ message }}
Repository navigation
Array.prototype.map() allows assigning additional properties to an array of defined type #36354
TypeScript Versions: from 3.5.1 to Nightly (issue not present in 3.3.3)
Search Terms:array map
Expected behavior: Third assignment should raise an error
Actual behavior: Assignment is allowed
Related Issues: No found any
Code
interfaceA{a: 2}// 1. Correct: b property is not allowed in type Aconsta: A={a: 2,b: 8}// 2. Correct: b property is not allowed in type Aconstother: A[]=[{a: 2,b: 8}]// 3. Incorrect (no error): why is b property permitted here?constarray: A[]=[1,2].map(i=>({a: 2,b: 8}))
Output
"use strict";// 1. Correct: b property is not allowed in type Aconsta={a: 2,b: 8};// 2. Correct: b property is not allowed in type Aconstother=[{a: 2,b: 8}];// 3. Incorrect (no error): why is b property permitted here?constarray=[1,2].map(i=>({a: 2,b: 8}));
The error in 3.3.3 is not the one you're looking for. That error is due to widening of 2 to number; it has nothing to do with the presence of the b property. Even this errors in 3.3.3:
constarray: A[]=[1,2].map(i=>({a: 2}));// error!// Type '{ a: number; }' is not assignable to type 'A'.
In reality the error you expect is not happening because excess property checking only happens in very specific circumstances. See #33158 for more discussion. Good luck!
Activity
jcalz commentedon Jan 22, 2020
Duplicate of #33158
The error in 3.3.3 is not the one you're looking for. That error is due to widening of
2tonumber; it has nothing to do with the presence of thebproperty. Even this errors in 3.3.3:In reality the error you expect is not happening because excess property checking only happens in very specific circumstances. See #33158 for more discussion. Good luck!
IllusionMH commentedon Jan 22, 2020
Or duplicate of #241
typescript-bot commentedon Feb 6, 2020
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.