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

Enhancement: [no-type-alias] allowTemplateLiterals #5095

Closed
4 tasks done
burtek opened this issue May 29, 2022 · 2 comments
Closed
4 tasks done

Enhancement: [no-type-alias] allowTemplateLiterals #5095

burtek opened this issue May 29, 2022 · 2 comments
Labels
enhancement: plugin rule option New rule option for an existing eslint-plugin rule package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin wontfix This will not be worked on

Comments

@burtek
Copy link
Contributor

burtek commented May 29, 2022

Before You File a Proposal Please Confirm You Have Done The Following...

My proposal is suitable for this project

  • I believe my proposal would be useful to the broader TypeScript community (meaning it is not a niche proposal).

Link to the rule's documentation

https://typescript-eslint.io/rules/no-type-alias

Description

Template Literals is a powerfull TypeScript feature, allowing to specify exact form a string may take. Using template literals allows to force specific input form, eg:

type JiraTicketNumber = `${'PROJ01' | 'PROJ02'}-${number}`
type ChatCommand = `/${keyof typeof commands}`
type FilterField = `${'include' | 'exclude'}: ${string}`

type Lang = 'en' | 'de' | 'es'
type Page = 'home' | 'blog' | 'store'
type URLSlug = `/${Lang}/${Page}`

but also allowing to mapping keys to another type (see examples in link above).

There might be (and in my case actually is) a case, where type aliasing is generally not allowed (apart from unions / intersections), with template literal being exception, as it's recommended to share the single definition of this type rather than have to use the template literal type in many different places, as that may lead to issues when the form of the template literal type changes.

I'd love to suggest adding new allowTemplateLiterals option that would handle such case. Template Literals are a new, unique form of typescript type and while there is an option to whitelist any other type (mapped types, generics, tuples, literals, etc), there is no real way to whitelist template literals.

Fail

// with allowTemplateLiterals: false (default)
type SlashCommand = `/${string}`

Pass

// with allowTemplateLiterals: true (default)
type SlashCommand = `/${string}`

Additional Info

For reference, #5092 (unmerged at the time of creating this issue) adds handling of type literals which are unhandled in current version of the plugin, see #5043

@burtek burtek added enhancement: plugin rule option New rule option for an existing eslint-plugin rule package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin triage Waiting for maintainers to take a look labels May 29, 2022
@JoshuaKGoldberg
Copy link
Member

👍 this makes sense, as a more restrictive option alternative to allowAliases.

@JoshuaKGoldberg JoshuaKGoldberg added accepting prs Go ahead, send a pull request that resolves this issue and removed triage Waiting for maintainers to take a look labels May 29, 2022
@Josh-Cena
Copy link
Member

This rule is now deprecated and we won't be adding new options.

@Josh-Cena Josh-Cena closed this as not planned Won't fix, can't repro, duplicate, stale Dec 17, 2023
@Josh-Cena Josh-Cena added wontfix This will not be worked on and removed accepting prs Go ahead, send a pull request that resolves this issue labels Dec 17, 2023
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 25, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement: plugin rule option New rule option for an existing eslint-plugin rule package: eslint-plugin Issues related to @typescript-eslint/eslint-plugin wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

3 participants