-
Notifications
You must be signed in to change notification settings - Fork 12.5k
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
Looking for an official type side (not user side) solution to the problem of no inference on string literals nested in object literals #42257
Comments
Further thoughts. If the reason no modifier has been provided thus far on the type side to fix this is because it's unsafe then let us have the option to be unsafe. We already have the options to set strict mode (or not) and some of us don't need that level of type safety. Let us back out and let our users use the language dynamically where they need to. Please consider providing a modifier on the type side that would increase the inference on string literals in this particular situation rather than asking our users to work around it with modifiers on their side. |
It doesn't, though, and it can't. You could induce an unsoundness here by a later legal unaliased mutation to type User = {
id: number;
name: string;
type: "member" | "editor" | "admin";
}
const LoginScreen = () => {
const bob = {
id: 1,
name: "Bob",
type: "member"
}
const getUser = (user: User) => {
if (user.name !== "member" && user.name !== "editor" && user.name !== "admin") {
throw new Error("Failed!");
}
// do whatever
return user.id;
}
// Argument of type '{ id: number; name: string; type: string; }' is not assignable to parameter of type 'User'.
// Types of property 'type' are incompatible.
// Type 'string' is not assignable to type '"member" | "editor" | "admin"'.(2345)
getUser(bob);
return { bob, getUser } ;
}
// Later, in a different module that TypeScript can't see when analyzing the prior code:
const p = LoginScreen();
p.bob.name = "quack";
// Throws
p.getUser(p.bob); |
Really appreciate the response Ryan. While using |
Gotcha, appreciate all of the useful feedback! Hopefully this thread is a little bit more searchable for the next dev who has this conundrum. |
This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes. |
Bug Report
🔎 Search Terms
prevent type widening in nested string literal in object literal inference
🕗 Version & Regression Information
This is a pre-existing issue and deals with the way that the compiler does type inference on object literals
⏯ Playground Link
Playground link with relevant code
💻 Code
🙁 Actual behavior
The string literal in bob was widened from
"member"
tostring
.🙂 Expected behavior
Since the compiler knows that
bob
is being passed intogetUser
and never mutated it should know thatbob
is is unambiguously of typeUser
and that the user of my type definitions doesn't need an explicit assertion or type onbob
.I don't want my users to have to assert
bob as const
or have toimport User
to explicitly defineconst bob: User ={...}
I want my users to be able to use dynamically typed variables AND have type inference on the string literals in my nested objects in situations when the compiler should know the intention behind the variable. Please consider:Please provide a type side solution to this issue rather than asking our users to work around it. Does one exist? If so, could you please define it in the TypeScript FAQ, as this problem is common, and googling for it is difficult due to misleading solutions that ask the user to assert the variable.
The text was updated successfully, but these errors were encountered: