-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
[no-base-to-string] False positive for tagged tepmplates #1757
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
Comments
happy to accept a PR here. |
I would like to try to create a PR for this. |
Hmm, @bradzacher would you mind elaborating on why we made this change behind a flag for "backwards compatibility"? To me, it seems like the lint just doesn't apply to template literals, since they aren't strings. e.g. take a look at this function with the default config: declare class Foo {}
declare function tag (xs: TemplateStringsArray, ...ys: Foo[]): void
const a = new Foo()
const b = new Foo()
tag`
a: ${a}
b: ${b}
` Here the only valid things to pass inside an interpolation is a Also, in this example, the error message is completely irrelevant 😄
I mean, it's true, but nothing is being stringified here 😄 Or maybe I'm missing something? 🤔 |
I wasn't thinking overly hard about it at the time. With a lot of issues and PRs being raised, I'm not always on the ball when thinking about solutions to things I'm not 100% familiar with. I haven't used custom template tags much, so on the surface I thought that there might be cases where you do want it to check tagged templates. But looking now, I realise that I'm flat out wrong. The case I was thinking of was when the arguments are typed as declare class Foo {}
declare function tag (xs: TemplateStringsArray, ...ys: string[]): void
const a = new Foo()
const b = new Foo()
tag`
a: ${a} // Argument of type 'Foo' is not assignable to parameter of type 'string'.
b: ${b} // Argument of type 'Foo' is not assignable to parameter of type 'string'.
` Seeing now that this option really shouldn't even exist, happy to accept a PR to switch it over to default |
My first thought was the same as @LinusU, but I added the option that @bradzacher suggested because I thought there might be and actually are tag functions that intentionally take raw(template: TemplateStringsArray, ...substitutions: any[]): string; , and in this case I think it is better to check that interpolated values can be stringified in meaningful ways. |
That's definitely a valid concern, but the fact that For tags custom to the codebase that use Two better approaches to solve this problem:
|
Repro
Expected Result
No lint errors because the tag does not necessarily stringify the interpolated values.
Actual Result
Lint error is reported.
2:7 error '{} will evaluate to '[Object object]' when stringified @typescript-eslint/no-base-to-string
Additional Info
Versions
@typescript-eslint/eslint-plugin
2.24.0
and2.24.1-alpha.0
@typescript-eslint/parser
2.24.0
and2.24.1-alpha.0
TypeScript
3.7.5
ESLint
6.8.0
node
12.14.1
npm
6.13.4
The text was updated successfully, but these errors were encountered: