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

A string literal type should not permit duplicate values #7498

Closed
NoelAbrahams opened this issue Mar 13, 2016 · 4 comments
Closed

A string literal type should not permit duplicate values #7498

NoelAbrahams opened this issue Mar 13, 2016 · 4 comments
Labels
Declined The issue was declined as something which matches the TypeScript vision Out of Scope This idea sits outside of the TypeScript language design constraints Suggestion An idea for TypeScript

Comments

@NoelAbrahams
Copy link

Hi,

Version: 1.8.5

I would like to see an error when duplicate values are found in a string literal declaration:

type alphabet = 'a' | ... | 'x' | 'y' | 'a';  // Error: Duplicate value 'a'

This is most likely an unintentional inclusion.

@kitsonk
Copy link
Contributor

kitsonk commented Mar 13, 2016

I think this actually would be a significant change in behaviour that would have unintentional impacts in a lot of ways. Types have always collapsed duplicates as long as they don't conflict.

For example, this has always been valid in TypeScript:

type Foo = { foo: string } | { foo: string } | { foo: string } | { foo: string };

@xLama
Copy link

xLama commented Mar 13, 2016

Union types permit duplicate values so I think it is by design.

#805

Basics

Some simple rules:
Identity: A|A is equivalent to A

@NoelAbrahams
Copy link
Author

Yes, I see that now.

I wonder whether a distinction can be made between constructing a union type vs. initialising one with duplicates.

type a1 = 'a'; 
type a2 = 'a';

type a = a1 | a2; // Okay

type a3 = 'a' | 'a';  // Error

I don't see why anyone would want to do the latter.

The use-case we have is with regard to a string literal type that has a growing number of values over time. We do not want a new software engineer to add a duplicate value to the list and go away thinking they can use the new value when it is already in the list.

@mhegazy
Copy link
Contributor

mhegazy commented Mar 14, 2016

This looks like a TSlint rule candidate. but it would be out of scope for the compiler.

@mhegazy mhegazy closed this as completed Mar 14, 2016
@mhegazy mhegazy added Suggestion An idea for TypeScript Out of Scope This idea sits outside of the TypeScript language design constraints Declined The issue was declined as something which matches the TypeScript vision labels Mar 14, 2016
@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Declined The issue was declined as something which matches the TypeScript vision Out of Scope This idea sits outside of the TypeScript language design constraints Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

4 participants