#weird problem with spread operator behavior?
23 messages · Page 1 of 1 (latest)
Preview:```ts
type Hello = {
some_prop?: string
}
const wrongProperty = {
wrong_property: "123",
}
function hello(hello: Hello) {}
hello({...wrongProperty})
hello({...wrongProperty, some_prop: "asd"})```
excess properties are allowed in some contexts, i guess spreading is one of them
looks like there's an issue for that
so,
type Hello = {
some_prop?: string
}
is a subset of the {} type
where {} isn't an empty object, it's a "thing" where I can access properties. so defining an object type says "look for at least these properties, but if it contains other properties that's fine"
this isn't exactly the issue here
literals shouldn't have excess properties:
function fn(x: { k: string }) {}
fn({ n: "" })
// ^^^^^
// Argument of type '{ n: string; }' is not assignable to parameter of type '{ k: string; }'.
// Object literal may only specify known properties, and 'n' does not exist in type '{ k: string; }'.
Preview:```ts
type Hello = {some_prop: string}
const wrongProperty = {wrong_property: "123"}
function hello(hello: Hello) {}
hello(Object.assign({some_prop: ""}, wrongProperty))```
but the spread gets around that incorrectly
this is a demonstration of the correct behavior of object values allowing excess properties
but object literals specifically should not
damn so I guess the only way to do this right now is to write all the props individually?
do what exactly?
make it so that props that don't belong are not allowed
i guess, yeah
or maybe the spread makes ts not use the object literal rules and that's expected, but don't see any comments of the sort on the issue
type K = { k: string }
function fn(x: K) {}
const wrong = { n: "", k: '' }
fn(wrong)
i see what you are saying chris
it works the way you'd expect if you provide a literal directly to the function
!resolved