Skip to content

TypeScript 3.7 Assertion Functions #7963

Closed
@jeremyBanks

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.

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions