question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeScript const assertions raise error in babel

See original GitHub issue

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

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:closed
  • Created 4 years ago
  • Reactions:4
  • Comments:13 (6 by maintainers)

github_iconTop GitHub Comments

5reactions
tanhauhaucommented, Apr 18, 2019

sorry for the late MR 😅

4reactions
DanielRosenwassercommented, Apr 3, 2019

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.

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found