-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
[Feature request]Should infer value from code block #24315
Comments
Note that for this precise case, you can write : const direction = row ? 'row' : 'column';
|
@s-ve Ah, I see. Thanks, silly me. Yes, this is more concise. Seems the pattern here is if it's an expression, it will get evaluated. |
So basically: declare function f(p: "a" | "b"): void;
let x = "a";
if (something) x = "b";
f(x); // Error TypeScript type inference tends to work forwards-only, so at |
@Andy-MS Thanks for the detail. So what about the error message? A better error might be:
The original error message is confusing and very long to interpreted. |
There is an existing suggestion that would make this error message a bit shorter : #24146. I'm not sure why typescript decides to generate this error message here. It would have been clearer if typescript chose to display something like Here is a shorter repro (Playground link) : interface FlexStyle {
flexDirection: 'column' | 'row'
}
const direction: string = 'column';
const x: FlexStyle | FlexStyle[] = {flexDirection: direction} // Error: Property 'length' is missing in type '{ flexDirection: string; }'. @Albert-Gao Could you maybe fill a separate issue for this problem ? |
There are dozens of bugs suggesting we guess at what the "best" error message to issue is, please do not add another |
Expected behavior:
It works
Actual behavior:
Now it will generate a very long and confusing error message. Why we need
Property 'length'
intype '{ flexDirection: string; }'.
?How to make it work currently
Modify
let direction = 'column'
tolet direction: 'column' | 'row' = 'column'
It's a very simple case, it should infer the value from the statement. Or at least, make that error message readable. In terms of type checking for React native, the error message are always like this. Seems not right, or I miss something here?
The text was updated successfully, but these errors were encountered: