New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Suggestion: perform excess property checks when spreading an inline object literal #39998
Comments
|
Absolutely agree with the suggestion here. We do stuff like this all the time: const company: Partial<Company> = {
...(update.name ? { name: update.name } : null),
...(update.email ? { email: update.email } : null),
...(update.alias ? { aliassssss: update.alias } : null), // no error
}; |
|
Just realised, this issue isn't just about excess property checks. We're also missing errors for non-excess properties (#41698): declare const someCondition: boolean;
type MyObject = { foo: number } & ({ bar: number } | {});
const a: MyObject = {
foo: 1,
// ✅ Error
bar: 'foo',
};
const b: MyObject = {
foo: 1,
...(someCondition
? {
// ❌ Expected error, got none
bar: 'foo',
}
: {}),
}; |
|
Is there any update on this? This is really annoying that ts doesn't check that |
|
Note that this issue isn't just on spreading immediately declared object literals, spreading a function parameter can introduce this issue: type Bar = {
a: string;
};
type Chaz = {
a: string,
c: "zip" | "zap"
}
function usesBar(value: Bar, c: "zip" | "zap") : Chaz {
return {
c,
...value, // The excess properties will clobber the c value!
};
}
const d = {
a: "hello",
c: "blah blah blah"
};
const e = usesBar(d, "zip");
console.log(e);
// {
// "c": "blah blah blah",
// "a": "hello"
// } |
|
same bug type Params = {
a?: any;
b?: any;
}
function func (params: Params) {
return;
}
const objWithAllowedKeys = { a: 2 }
const objWithForbiddenKeys = { c: 2 }
func({ ...objWithAllowedKeys }) // valid - no error ✅
func({ ...objWithForbiddenKeys }) // valid - error ✅
func({ ...objWithAllowedKeys, ...objWithForbiddenKeys }) // invalid - why no error? ❌ |
|
Any update on this? |
Similar to #3499, but i did a search for other places props was used literally in -web We'd get a typescript error for these if microsoft/TypeScript#39998 was fixed, but that doesn't seem to be on their table yet
* fix(hooks-web): don't pass widget props to ui components Similar to #3499, but i did a search for other places props was used literally in -web We'd get a typescript error for these if microsoft/TypeScript#39998 was fixed, but that doesn't seem to be on their table yet * Update packages/react-instantsearch-hooks-web/src/widgets/HitsPerPage.tsx Co-authored-by: François Chalifour <francoischalifour@users.noreply.github.com> Co-authored-by: François Chalifour <francoischalifour@users.noreply.github.com>
TypeScript Version: 3.9.2
Search Terms:
Code
In the example above, I only want to include specific properties when a condition is met. That's the only reason I'm using spread here.
I understand that TypeScript only performs excess property checks inside of object literals. Currently this does not include inline object literals which are being spread inside of another object literal.
In my experience this is a very common code pattern so it would be great if TypeScript handled this.
Expected behavior:
An error
Actual behavior:
No error
Playground Link:
Related Issues:
The text was updated successfully, but these errors were encountered: