Skip to content

Array.prototype.map() allows assigning additional properties to an array of defined type #36354

Closed
@esanzgar

Description

@esanzgar

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

interface A {
    a: 2
}

// 1. Correct: b property is not allowed in type A
const a: A = { a: 2, b: 8 }

// 2. Correct: b property is not allowed in type A
const other: A[] = [{ a: 2, b: 8 }]

// 3. Incorrect (no error): why is b property permitted here?
const array: A[] = [1, 2].map(i => ({ a: 2, b: 8 }))
Output
"use strict";
// 1. Correct: b property is not allowed in type A
const a = { a: 2, b: 8 };
// 2. Correct: b property is not allowed in type A
const other = [{ a: 2, b: 8 }];
// 3. Incorrect (no error): why is b property permitted here?
const array = [1, 2].map(i => ({ a: 2, b: 8 }));
Compiler Options
{
  "compilerOptions": {
    "noImplicitAny": true,
    "strictNullChecks": true,
    "strictFunctionTypes": true,
    "strictPropertyInitialization": true,
    "strictBindCallApply": true,
    "noImplicitThis": true,
    "noImplicitReturns": true,
    "useDefineForClassFields": false,
    "alwaysStrict": true,
    "allowUnreachableCode": false,
    "allowUnusedLabels": false,
    "downlevelIteration": false,
    "noEmitHelpers": false,
    "noLib": false,
    "noStrictGenericChecks": false,
    "noUnusedLocals": false,
    "noUnusedParameters": false,
    "esModuleInterop": true,
    "preserveConstEnums": false,
    "removeComments": false,
    "skipLibCheck": false,
    "checkJs": false,
    "allowJs": false,
    "declaration": true,
    "experimentalDecorators": false,
    "emitDecoratorMetadata": false,
    "target": "ES2017",
    "module": "ESNext"
  }
}

Playground Link: Provided

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions