Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bad reporting of unintentional comparison #28658

Closed
johndeighan opened this issue Mar 28, 2025 · 2 comments
Closed

Bad reporting of unintentional comparison #28658

johndeighan opened this issue Mar 28, 2025 · 2 comments
Labels
tsc related to the TypeScript tsc compiler upstream Changes in upstream are required to solve these issues

Comments

@johndeighan
Copy link

$ deno --version
deno 2.2.5 (stable, release, x86_64-pc-windows-msvc)
v8 13.5.212.4-rusty
typescript 5.7.3

This bug also exists in the TypeScript compiler - I don't know if you use it directly or have your own code. In the following TypeScript file, named bug.ts:

export enum SynCat {
	INDENT,
	UNDENT
	}

export type Token = string | SynCat

function fileHandler(lTokens: Token[]): void {

	if (lTokens[0] === SynCat.INDENT) {
		lTokens.shift()
		while (lTokens[0] !== SynCat.UNDENT) {
			const tok = lTokens.pop()
		}
	}
	return
}

When I run deno test bug.ts, I get the message:

TS2367 [ERROR]: This comparison appears to be unintentional because the types 'SynCat.INDENT' and 'SynCat.UNDENT' have no overlap.
                while (lTokens[0] !== SynCat.UNDENT) {
                       ~~~~~~~~~~~~~~~~~~~~~~~~~~~~

I think it's assuming that lTokens[0] has value SynCat.INDENT because of the preceding if check. However, inside that if statement that value of lTokens is modified by shifting off a value, and therefore, lTokens[0] has been modified. So, the comparison of lTokens[0] with SynCat.UNDENT is valid, i.e. not superfluous.

I suspect that the code will still run, but the message is wrong (unless I'm missing something). Also, it's reported as an ERROR, not a WARNING.

I did a search of TypeScript issues, and this may be related to #9998.

@marvinhagemeister
Copy link
Contributor

This bug also exists in the TypeScript compiler - I don't know if you use it directly or have your own code.

Yeah we're using the TypeScript compiler directly for type checking. We don't have our own code for that.

@marvinhagemeister marvinhagemeister added upstream Changes in upstream are required to solve these issues tsc related to the TypeScript tsc compiler labels Mar 28, 2025
@marvinhagemeister
Copy link
Contributor

Closing as there isn't much we can do on our end. In general we don't extend the TypeScript language and leave that up to the TypeScript team. The upstream issue is here: microsoft/TypeScript#9998

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
tsc related to the TypeScript tsc compiler upstream Changes in upstream are required to solve these issues
Projects
None yet
Development

No branches or pull requests

2 participants