Skip to content

TypeScript 3.7 Assertion Functions #7963

Closed

Description

@jeremyBanks

#7940 (TypeScript 3.7 support) was closed as a dupe of #7431 (optional chaining and nullary coalescing support). However, TypeScript 3.7 had another syntax change that wasn't included in that: assertion functions. Unlike the other syntax changes, this isn't being standardized as part of regular JavaScript, which might be why there doesn't seem to be a Babel plugin for it.

I've been looking forward to using this feature. Will it be possible to support it in CRA?

Example Output

mkdir example;
cd example;
npx create-react-app . --typescript;
echo '
function assertIsString(val: any): asserts val is string {
    if (typeof val !== "string") {
        throw new AssertionError("Not a string!");
    }
}
' > src/App.tsx;
yarn build;
yarn run v1.17.3
$ react-scripts build
Creating an optimized production build...
=============

WARNING: You are currently running a version of TypeScript which is not officially supported by typescript-estree.

You may find that it works just fine, or you may not.

SUPPORTED TYPESCRIPT VERSIONS: >=3.2.1 <3.7.0

YOUR TYPESCRIPT VERSION: 3.7.2

Please only submit bug reports when using the officially supported version.

=============
Failed to compile.

./src/App.tsx
SyntaxError: /home/jeremy/example/src/App.tsx: Unexpected token, expected "{" (1:43)

> 1 | function assertIsString(val: any): asserts val is string {
    |                                            ^
  2 |     if (typeof val !== "string") {
  3 |         throw new AssertionError("Not a string!");
  4 |     }


error Command failed with exit code 1.
info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.

Activity

  1. self-assigned this
    on Nov 11, 2019
  2. added this to the 3.3 milestone on Nov 11, 2019
  3. ianschmitz commented on Nov 11, 2019

    @ianschmitz
    Contributor

    I'll look into this and get back to you whether it's supported.

  4. ianschmitz commented on Nov 12, 2019

    @ianschmitz
    Contributor

    Looks like this should be supported via Babel and typescript-eslint

    Here's the reference for TypeScript 3.7 support in Babel: https://github.com/babel/babel/milestone/29?closed=1. Looks like we may have to bump the packages once more before releasing here as one PR was merged only hours ago.

    Here's typescript-eslint: typescript-eslint/typescript-eslint#1045

  5. SQReder commented on Nov 22, 2019

    @SQReder

    @ianschmitz look like both babel and typescript-eslint done with referenced PRs. Any updates for CRA?
    We want to use assertion feature and AFAIK react-scripts is the last stopper.

  6. locked and limited conversation to collaborators on Nov 30, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions