Implementing "Union Type" for specific string values #8459
Unanswered
ABNERMATHEUS
asked this question in
Language Ideas
Replies: 2 comments 4 replies
-
See: #2849 |
Beta Was this translation helpful? Give feedback.
0 replies
-
I think using Enum is much more better than string like enum Status(Pending, Received, Prgoress); Also if we can use string as Enum values, it will be more usefull: enum Status {
Pending = "pending",
Received = "received",
Progress = "progress"
} and in concise way: enum Status(Pending = "pending", Received = "received", Prgoress = "progress"); |
Beta Was this translation helpful? Give feedback.
4 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Issue: Implement Implementing "Union Type" for specific string values without using Enum
Description:
In TypeScript, it is possible to define a set of specific values for a variable using union types, as shown in the example below:
type Status = 'pending' | 'received';
This approach is straightforward, avoids the creation of an enum, and can be used directly in string-type variables, keeping the code clean.
Currently, in C#, the common solution for this scenario is the creation of enums. However, creating an enum can sometimes be excessive when only a small number of fixed string values is needed. Additionally, enums restrict the flexibility of using string values directly.
Proposal:
Implement a simple way to define fixed string values without the need for enums to restrict the possible values that a string can hold.
public 'received' | 'pending' | 'progress' Status { get; set; }
Beta Was this translation helpful? Give feedback.
All reactions