Skip to content

satisfies alters type inference of some (generically-typed?) variable assignments #56623

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
stwlam opened this issue Nov 30, 2023 · 6 comments
Closed

Comments

@stwlam
Copy link

stwlam commented Nov 30, 2023

πŸ”Ž Search Terms

satisfies narrows generic assignment

πŸ•— Version & Regression Information

This is the behavior in every version I tried (from 4.9.5), and I reviewed the FAQ for entries about Generics

⏯ Playground Link

https://www.typescriptlang.org/play?ts=5.3.2#code/C4TwDgpgBAChxQLxQEQGMCGwVQD6oBMB7AcxQG4AoNIgOwGcFJh6koBtdLFAGkNJQBdKPSwBLegDMxEVnGDtBVZvQB0Y2mgA2AVwKyAFCgBGYgE4EUASigB6W1FpEERANZA

πŸ’» Code

type Pet = "cat" | "dog";
const pets = ["cat", "dog"] satisfies Pet[];
pets.includes("bird") // not ok

πŸ™ Actual behavior

pets is of type Pet[]

πŸ™‚ Expected behavior

pets is of type string[]

Additional information about the issue

No response

@fatcerberus
Copy link

Yes, satisfies induces contextual typing, so it can sometimes affect the type of an array or object literal. If it didn’t, then the above would fail because string[] isn’t assignable to Pet[].

@stwlam
Copy link
Author

stwlam commented Nov 30, 2023

I suppose the announcement reads a little off, then: perhaps when it's documented it could be clarified further?

The new satisfies operator lets us validate that the type of an expression matches some type, without changing the resulting type of that expression.

@fatcerberus
Copy link

I agree that should be clarified, but to be fair, technically if pets is allowed to be widened to string[] then it no longer satisfies Pet[] right? πŸ˜‰

@stwlam
Copy link
Author

stwlam commented Nov 30, 2023

I assumed it would confirm that the value is assignable to the satisfies type, narrowing as necessary, but then returning the actual type assignment to normal inferencing procedure.

@jasikpark
Copy link

I think I would consider the strict array.includes behavior more the issue, no? a la https://github.com/total-typescript/ts-reset

@jcalz
Copy link
Contributor

jcalz commented Nov 30, 2023

Duplicate of #55189… the documentation is indeed misleading here

@stwlam stwlam closed this as not planned Won't fix, can't repro, duplicate, stale Nov 30, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants