Skip to content
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

satisfies should work when an object is spread into another #51569

Closed
arcticmatt opened this issue Nov 17, 2022 · 6 comments
Closed

satisfies should work when an object is spread into another #51569

arcticmatt opened this issue Nov 17, 2022 · 6 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@arcticmatt
Copy link

Suggestion

It looks like satisfies doesn't work when you spread an object into another

TypeScript errors as expected

type Colors = "red" | "green" | "blue";

// Ensure that we have exactly the keys from 'Colors'.
const favoriteColors = {
    "red": "yes",
    "green": false,
    "blue": "kinda",
    "platypus": false
//  ~~~~~~~~~~ error - "platypus" was never listed in 'Colors'.
} satisfies Record<Colors, unknown>;

TypeScript does not error, but I think it should

type Colors = "red" | "green" | "blue";

const foo = {
    "platypus": false
}

// Ensure that we have exactly the keys from 'Colors'.
const favoriteColors = {
    "red": "yes",
    "green": false,
    "blue": "kinda",
    ...foo
} satisfies Record<Colors, unknown>;

I would expect the latter to also have a TypeScript error, to prevent favoriteColors from having undesired extra properties.

Why? Well, because sometimes passing extra properties can break stuff.

🔍 Search Terms

satisfies, spread

@fatcerberus
Copy link

fatcerberus commented Nov 17, 2022

Duplicate of #50938; interestingly the examples are almost identical.

See also #39998

@fatcerberus
Copy link

fatcerberus commented Nov 17, 2022

Why? Well, because sometimes passing extra properties can break stuff.

Just FWIW, you probably shouldn't rely on this check to prevent runtime errors due to excess properties. It's a fairly limited lint check primarily designed to catch typos in object literals; it's still very easy to end up with extra properties:

const faves = {
    "red": "yes",
    "green": false,
    "blue": "kinda",
    "platypus": false
};
const favoriteColors = faves satisfies Record<Colors, unknown>;  // no error

@RyanCavanaugh RyanCavanaugh added the Working as Intended The behavior described is the intended behavior; this is not a bug label Nov 17, 2022
@RyanCavanaugh
Copy link
Member

Spreads aren't subject to excess property checks and it's not clear to me that they ever could be in an ergonomic way.

@fatcerberus
Copy link

@RyanCavanaugh If this is Working As Intended, shouldn't #39998 be closed then? 😉

@RyanCavanaugh
Copy link
Member

Awaiting More Feedback isn't the same as closed? 🙃

@typescript-bot
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

4 participants