I'm confused as to why Typescript isn't flagging this as an error...
When I conditionally add a new property to an object that doesn't exist in the type definition, Typescript still allows it
type Filters = {
keywords: Array<string>
}
const condition = 1;
let filters: Filters = {keywords: ['keyword']}
filters = {
...filters,
...(condition && {...{ tags: ['tag']}}),
}
Results:
filters: {
keywords:["keyword"]
tags:["tag"]
}
However, I was expecting the following error:
Object literal may only specify known properties, and 'tags' does not exist in type 'Filters'.
Note: I do get the expected error when trying to add the same property like this:
filters = {...filters, tags: ['tag']}