TypeScript const assertions raise error in babel
See original GitHub issueBug 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
function foo() {
const a = getObjectAWithLongAssType();
const b = getObjectBWithLongAssType();
return [a, b] as const; // no errors in tsc or vscode
// Previously: return [a,b] as [typeof a, typeof b]
}
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)
"@babel/core": "^7.4.0",
"@babel/plugin-proposal-class-properties": "^7.4.0",
"@babel/polyfill": "^7.4.0",
"@babel/preset-env": "^7.4.2",
"@babel/preset-react": "^7.0.0",
"@babel/preset-typescript": "^7.3.3",
Possible Solution
@tanhauhau is there a need for tsCheckLiteralForConstantContext
to happen in babel? To me it seems like it’s enough to parse as const
expressions as assertions and move on, since TypeScript is actually in charge of ensuring the assertions are valid.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:4
- Comments:13 (6 by maintainers)
Top Results From Across the Web
7.7.0 Released: Error recovery and TypeScript 3.7 - Babel.js
Today we are releasing Babel 7.7.0! This release includes new parser features like top-level await ( await x() , Stage 3) and Flow...
Read more >Why does Babel not properly handle const? - Stack Overflow
Ergo Babel does properly handle const. The reason why your code wasn't transpiled to throw an error is because of part 1.
Read more >Deep dive into enums, const assertions and union types
In this blog, I will talk about enums, const assertions and union types ... Left = "LEFT" , Typescript would throw an error...
Read more >const assertions are the killer new TypeScript feature
In addition to logging Redux actions and state, LogRocket records console logs, JavaScript errors, stacktraces, network requests/responses with ...
Read more >Documentation - TypeScript 3.7
There's a specific set of functions that throw an error if something unexpected happened. They're called “assertion” functions. As an example, Node.js has...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
sorry for the late MR 😅
Yeah,
as const
should be very permissive in the syntax which it parses. None of the semantic restrictions should be applied to Babel if it’s just going to get erased anyway.