Skip to content

Check for optional property expanded with spread operator is skipped #56114

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

Closed
085astatine opened this issue Oct 16, 2023 · 2 comments
Closed
Labels
Duplicate An existing issue was already created

Comments

@085astatine
Copy link

πŸ”Ž Search Terms

"spread operator", "optional property"

πŸ•— Version & Regression Information

  • This is the behavior in every version in Playground.

⏯ Playground Link

https://www.typescriptlang.org/play?#code/JYOwLgpgTgZghgYwgAgCJzHZBvAUMg5GAe2IC5kBnMKUAcwG59CAjOKC62kR3AX1y5QkWIhQB5AA5hgxEHAA26TDmYES5KjXpNCyNlAD8nbTyYDcAekvIAwnEooAjBQCiUKMShWbAPmQACp6S0GAAnsgA5AaRyMCUyAC28ZT0cSDI4SFR2PpwAF7GWtx0yAA+yACuIAAmEDCgEDUMRKQmJS18sSyVYMhQEACOlcADNemZYdmRynCRAHS4CHLUyABuipUQLmgYWAC8qnoaFJEAYuLikQA0asjzD9hs+RQARABC7K98t3xMPnYHCgAEwUAByxGQ7k8UGQhnhSxWfQ2Ci2oOQUhkckUs2QhzwxzaUTOpBudwe8yeBTenyg31+-2sgMcyAAzG4PF4Af4ACpTFCRXInYo6PIvEVmZBdOIJEDEPoOVJ0eQsBQoMCQrICzGyeRKPYLbkYlgAKwgCD6CmAIkUSTgETkCgilBCCGAMAiAGs5QB3DKSYKhYAQSjXZBwWpRZ6xGrEEPIOV9CAAD3ifVAk2mOux+swhuWIFWKK27Ix0l1OL2eKOhGF51Jtz0z1OtLJf2YQA

πŸ’» Code

interface Data {
    foo: string;
    bar: string;
}

interface OptionalData {
    foo: string;
    bar?: string;
}

// Case 1: Error
// > Property 'bar' is missing in type '{ baz?: string | undefined; foo: string; }' but required in type 'Data'.
const value1: Data = {
    foo: 'FOO',
    ...{baz: "Bar"},
};

// Case 2: No Error ???
const value2: OptionalData = {
    foo: 'Foo',
    ...{baz: "Bar"},
};

// Case 3: Error
// > Type '{ foo: string; baz: string; }' is not assignable to type 'OptionalData'.
// > Object literal may only specify known properties, and 'baz' does not exist in type 'OptionalData'.
const value3: OptionalData = {
    foo: 'Foo',
    baz: 'Bar',
};

πŸ™ Actual behavior

In all cases, bar is misspelled as baz

In cases 1 and 3, typos can be detected as errors.
However, in case 2, the compilation succeeds without any errors.

πŸ™‚ Expected behavior

In all cases, typos can be detected as errors.

Additional information about the issue

I found this bug when I was writing the following code with exactOptionalPropertyTypes set to true.

return {
    foo,
    ...(bar !== undefined ? { bar } : {}),
};
@fatcerberus
Copy link

The Excess Property Check1 is not (currently) done for spreads. See #39998.

Footnotes

  1. Note that, in general, EPC is primarily designed as a lint check and is intentionally not comprehensive. ↩

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Oct 16, 2023
@typescript-bot
Copy link
Collaborator

This issue has been marked as "Duplicate" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Oct 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants