-
-
Notifications
You must be signed in to change notification settings - Fork 5.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
TypeScript const assertions raise error in babel #9800
Comments
Hey @ssynix! We really appreciate you taking the time to report an issue. The collaborators If you need any help, or just have general Babel or JavaScript questions, we have a vibrant Slack |
hmm.. @nicolo-ribaudo need your advice |
Ok, I downloaded TS 3.4 and apparentely we only need to check the top-level node. e.g. This isn't valid: (a ? 0 : 1) as const; but this is: [a ? 0 : 1] as const; |
These are also valid: const obj = {
readonly: [1, 'foo'] as const,
array: [1, 'foo']
};
const array = [1, 'foo', [1, 'foo'] as const] as const; From the announcement:
It doesn't seem to put any constraints on the members of those array/object literals, or that Does babel typically try to validate types, or simply parse them? To me |
I don't think so. If it wasn't syntax-related, those two snippets would be equivalent: const foo = [1, 2, 3] as const; const _tmp = [1, 2, 3] as const;
const foo = _tmp as const; But the first is valid; the second isn't. What the "argument" to as-const is syntax-related, not type related. |
The syntax is valid TypeScript, except that it contains typing errors. It's feasible to imagine a TypeScript version that will add support for Same with We don't want to duplicate the type-checker's work in the Babel parser itself, since the TypeScript compiler is already in charge of that. Babel should emit code as if the types don't exist, and using an incorrect typing assertion should not get in the way of that. That's my 2 cents based on the limited knowledge I have of the internals of babel, so definitely take it with a grain of salt! The main issue here is that valid const assertions are being rejected by babel in a few cases. |
Yeah, |
sorry for the late MR 😅 |
Could someone update the version of |
It is great that this is merged, but is there a timeline for when it will also be released? |
The fix has been released in @babel/parser 7.4.4. 13 days ago. Check https://www.npmjs.com/package/@babel/parser/v/7.4.4 and https://github.com/babel/babel/releases/tag/v7.4.4 for more details. |
Should following work?
Because I am getting unexpected token error with @babel/parser@7.5.5 |
Bug Report
Current Behavior
Some const assertions result in errors. Seems like the check introduced in this PR is too strict? Because I didn't see any errors in VSCode till I started webpack dev server.
Input Code
Expected behavior/code
VSCode outputs the expected readonly tuple type, so I don't expect any errors in babel.
Babel Configuration (.babelrc, package.json, cli command)
Possible Solution
@tanhauhau is there a need for
tsCheckLiteralForConstantContext
to happen in babel? To me it seems like it's enough to parseas const
expressions as assertions and move on, since TypeScript is actually in charge of ensuring the assertions are valid.The text was updated successfully, but these errors were encountered: