Skip to content

The removal of TypeScript types may result in broken JavaScript code #56597

Closed
@BlueNebulaDev

Description

@BlueNebulaDev

Version

v23.2.0

Platform

Linux 6.12.9 Fri Dec 20 10:11:49 UTC 2024 x86_64 GNU/Linux

Subsystem

No response

What steps will reproduce the bug?

Run the following TypeScript script with nodejs (with the --experimental-strip-types option, if necessary):

function mkId() {
	return <T>
		(x: T)=>x;
}

const id = mkId();
console.log(id(5));

How often does it reproduce? Is there a required condition?

It always happen deterministically.

What is the expected behavior? Why is that the expected behavior?

The script should print 5.

What do you see instead?

The script errors with:

console.log(id(5));
            ^

TypeError: id is not a function

Additional information

I believe that Node strips TS types in such a way that the script I showed gets turned into the following JS code:

function mkId() {
	return
		(x: T)=>x;
}

const id = mkId();
console.log(id(5));

And unfortunately Node interprets a return followed by a new line as a return;.

The same behavior happens with different (but similar code), for instance:

function mkId() {
	return <
		T
	>(x: T)=>x;
}

It is somewhat common to break templates on a new line, when the generic types are complex (for instance if they have long extend clauses) and this issue is particularly annoying because the IDE (which understands TS) doesn't flag it as an error.

Metadata

Metadata

Assignees

No one assigned

    Labels

    confirmed-bugIssues with confirmed bugs.strip-typesIssues or PRs related to strip-types support

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions