Skip to content

TS1166 reported for imported symbol as computed property name in generated d.ts #61360

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

Closed
jeremymeng opened this issue Mar 5, 2025 · 3 comments

Comments

@jeremymeng
Copy link
Member

🔎 Search Terms

ts1166

🕗 Version & Regression Information

  • This changed between versions 5.7.2 and 5.8.2
  • This changed in commit or PR 61136
  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about _________
  • I was unable to test this on prior versions because _______

⏯ Playground Link

https://www.typescriptlang.org/play/?ts=5.7.3#code/JYWwDg9gTgLgBAbzsAdgZzAUwMbwL5wBmUEIcARChACaYBcArjMADbkDcAUJ9hOvNgZoYpOAF5k6LLgB0g4aS6dMAD0iw42FgEM0aOAFkAngFEoJKHFUxMKavrMXEnOHADa8kSAC6ACgCUdHDCUKgA5s6urlCYMAxQKBQUXK54nGmcQA

💻 Code

compiling this code with 5.8.2

import { inspect } from "node:util";
const custom = inspect.custom;
export class MyError extends Error {
  [custom](): string {
    return " ";
  }
}

🙁 Actual behavior

generates the d.ts of

declare const custom: symbol;                                                                                                                                                                                              A computed property name in a class property declaration must have a simple literal type or a
export declare class MyError extends Error {                                                                                                                                                                                                                                                'unique symbol' type. [1166]
    [custom]: () => string;
}
export {};
//# sourceMappingURL=restError.d.ts.map

Opening this in editor we are seeing error of

restError… 3 5 error 1166 A computed property name in a class property declaration must have a simple literal type or a 'unique symbol' type. (lsp)

🙂 Expected behavior

d.ts generated by 5.7.2 is slightly different [x: symbol]: () => string; and has no errors

export declare class MyError extends Error {
    [x: symbol]: () => string;
}
//# sourceMappingURL=restError.d.ts.map

Additional information about the issue

every-ts bisect pointing to this commit:

c3ae7c4 is the first bad commit
commit c3ae7c4 (HEAD)
Author: Gabriela Araujo Britto [email protected]
Date: Fri Feb 7 10:55:34 2025 -0800

Revert return type narrowing (#61136)

Same issue exists in version 5.9.0-dev.20250305

@Andarist
Copy link
Contributor

Andarist commented Mar 6, 2025

Compiling this code in TS 5.8 (TS playground):

const sym = Symbol();
const symAlias = sym;

const str: string = "";

export class MyError extends Error {
  [str]: number | undefined;
  [symAlias]: number | undefined;
}

Produces this DTS output (TS playground):

declare const symAlias: symbol;
declare const str: string;
export declare class MyError extends Error {
    [str]: number | undefined;
    [symAlias]: number | undefined;
}
export {};

And it doesn't have any errors in TS 5.8. It does error in TS 5.7 though: TS playground.

To the best of my knowledge this works as intended. TypeScript doesn't guarantee that a declaration file generated by a newer version of TS will stay compatible with older versions. This has been called out in the release notes as a potential breaking change:

Note that it’s possible (though unlikely) that a file compiled in TypeScript 5.8 may generate a declaration file that is not backward compatible in TypeScript 5.7 or earlier.

@jakebailey
Copy link
Member

The bisect must have gone wrong; this was definitely changed by #60052.

This was an unavoidable break; there was just no way to improve the handling of computed properties in declaration files without breaking backwards compatibility for older versions of TS that are lacking the feature.

IIRC in 5.7 at least, the error can be "ignored" and technically work with the right behavior, though that's not really a workaround.

@jeremymeng
Copy link
Member Author

closing as it's intended.

jeremymeng added a commit to jeremymeng/azure-sdk-for-js that referenced this issue Mar 10, 2025
as 5.8 introduces some changes that cause generated .d.ts files incompatible
with previous versions of TypeScript compiler:

microsoft/TypeScript#61360 (comment)

This PR temporarily pinns its version to ~5.7.3 until we can upgrade to v5.8
jeremymeng added a commit to Azure/azure-sdk-for-js that referenced this issue Mar 11, 2025
as 5.8 introduces some changes that cause generated .d.ts files incompatible
with previous versions of TypeScript compiler:

microsoft/TypeScript#61360 (comment)

This PR temporarily pins `typescript` version to ~5.7.3 until we can upgrade to v5.8


### Issues associated with this PR

tracking upgrading to 5.8:
#33324

### What are the possible designs available to address the problem? If
there are more than one possible design, why was the one in this PR
chosen?

We could upgrade to 5.8, but the incompatible output would cause a lot
of warnings from api-extractor because it still bundles 5.7.3.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants