Skip to content

[restrict-template-expressions] Missing handling of intersection type #1797

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
ulrichb opened this issue Mar 25, 2020 · 3 comments · Fixed by #1803
Closed

[restrict-template-expressions] Missing handling of intersection type #1797

ulrichb opened this issue Mar 25, 2020 · 3 comments · Fixed by #1803
Labels
enhancement New feature or request good first issue Good for newcomers has pr there is a PR raised to close this package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin

Comments

@ulrichb
Copy link
Contributor

ulrichb commented Mar 25, 2020

Repro

{
  "rules": {
    "@typescript-eslint/restrict-template-expressions": "error"
  }
}
type MyString = string & { kind: "MyString" };

const myString = "x" as MyString;

console.log(`myString: ${myString}`); // False positive "Invalid type of template ..."

Expected Result

No error.

Actual Result

"Invalid type of template literal expression"

Additional Info

Real-world example:

import { Term } from "sparqljs";

const someTerm = "x" as Term;

console.log(`someTerm: ${someTerm}`);

See https://github.com/DefinitelyTyped/DefinitelyTyped/blob/638a579eecfa0486115f2440518603f25a8b58b7/types/sparqljs/index.d.ts#L223.

Versions

package version
@typescript-eslint/eslint-plugin 2.25.0
@typescript-eslint/parser 2.25.0

Proposed solution

Don't emit an error if any item of the intersection type is "allowed" in respect to the options.

@ulrichb ulrichb added package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for team members to take a look labels Mar 25, 2020
@bradzacher bradzacher added enhancement New feature or request good first issue Good for newcomers and removed triage Waiting for team members to take a look labels Mar 25, 2020
@bradzacher
Copy link
Member

Happy to accept a PR!
This wasn't handled because intrinsically, the type is not the primitive type.

@ulrichb
Copy link
Contributor Author

ulrichb commented Mar 25, 2020

This wasn't handled because intrinsically, the type is not the primitive type.

Yes that's correct, but this pattern (add a "fake" disciminator/tag prop) is nice to "type" primitives (forbid assignability between string / MyStringA / MyStringB).

@bradzacher
Copy link
Member

bradzacher commented Mar 25, 2020

yeah, I use this within the parser here to help distinguish between known-canonicalized paths, and other paths.

It would be a good change to make, we should probably create some utilities for this, because this will effect many of our rules.

It's not a "common" pattern, so it's not been a problem for most users.

ulrichb added a commit to ulrichb/typescript-eslint that referenced this issue Mar 26, 2020
ulrichb added a commit to ulrichb/typescript-eslint that referenced this issue Mar 26, 2020
ulrichb added a commit to ulrichb/typescript-eslint that referenced this issue Mar 26, 2020
@bradzacher bradzacher added the has pr there is a PR raised to close this label Mar 26, 2020
ulrichb added a commit to ulrichb/typescript-eslint that referenced this issue Mar 27, 2020
@github-actions github-actions bot locked as resolved and limited conversation to collaborators May 15, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request good first issue Good for newcomers has pr there is a PR raised to close this package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin
Projects
None yet
2 participants