Skip to content

Array Mapping Allows Unknown Properties #45979

@GeoMarkou

Description

@GeoMarkou

Bug Report

🔎 Search Terms

array map properties spread operator

🕗 Version & Regression Information

Always

Please keep and fill in the line that best applies:

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about Array Mapping Allows Unknown Properties

⏯ Playground Link

https://www.typescriptlang.org/play?ts=4.5.0-dev.20210910#code/JYOwLgpgTgZghgYwgAgArQM4HsTIN4BQyxyMwUGYAcnALYQBcylUoA5gNxEkYCuUIOo2ZhWITgQC+BAghyVScMAAtoDdBRzIAvPm7EyFakKYByAFJZlIUwBp9zfoPpmAyrWArTUrrPlhmHHVMLV1CEmQAOmj4FWh7CIB6ROQAYSwoKAgEMAAbAE9kaCgMjAcAMXJKGghaM0trZHMoSO9JXzkQBQRlYFyAEyyQYM0QAG0AXR1kMYsrXGa7ZFNXXgw4GwnI2jgABwAeDWwQAD4ACjOIXIhbZGAASh0T5DPwkmjI2NUoBJJk5AA6spCv0sBAMDYAipgBgipkMgB+CpVYy1JhXCBSe73IA

💻 Code

interface Person {
    firstName: string;
    surname: string;
}

const father:Person = {
    firstName: 'John',
    surname: 'Smith'
};

const son:Person = {
    ...father,
    // Correctly errors
    FirstNaem: 'John Jr.'
};

const children:Person[] = ['John Jr', 'Susan'].map<Person>((ele, i) => ({
    ...father,
    // Why doesn't this error?
    FirstNaem: ele
}))

🙁 Actual behavior

Mapping an array of Person above lets you have typos in properties and define new properties

🙂 Expected behavior

The map function at the end should have the same error as when defining const son

Type '{ FirstNaem: string; firstName: string; surname: string; }' is not assignable to type 'Person'.
Object literal may only specify known properties, but 'FirstNaem' does not exist in type 'Person'. Did you mean to write 'firstName'?(2322)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions