I don't understand why Typescript doesn't highlight this as an error ...
When I conditionally add to an object a new property that doesn't exist in that object type definition, Typescript does assign 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"]
}
While I expected to get this error:
Object literal may only specify known properties, and 'tags' does not exist in type 'Filters'.
PS: I get the expected error when I try to add the same property this way:
filters = {...filters, tags: ['tag']}