Replies: 1 comment 1 reply
-
To be clear, are there any constraints on the types of the referenced vars? for example:
|
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Type Literals / Enums / Constants
Problem statement
We have introduce Type Literals some time ago and it is really good as it allows us to support different patterns like Enumerations or Value Discrimintators.
Let's take an example
This script validates as we are invoking the
request
function with "GET" as it is part of the Union options. And if we provide a wrong option likerequest("test")
it will fail withNot only it helps you with type checking information our tools understand the types and autocompletion will suggest the correct literals.
My only problem with the current approach that I don't suffer in Java, scala, rust or any other language that has enums is that I need to duplicate the string "GET" too many times
case is "GET" -> "Getting something"
,request("GET")
andtype HTTP_METHOD = "GET"
. We can improve the code by introducing a constantAnd "fix" the issue but the problem is still there, if tomorrow I need to change that value I need to modify it in two different places.
Proposed Solution
The proposed solution is to be able to reference literal vars as literal types. Let's change the code
With this new change I don't longer need to declare the
GET_METHOD_TYPE
as a var with a constant can be used as a Type Literal expression and so change it changes everywhere.What kind of vars can be used?
Only literal expression that are :
String, Boolean or Number
If it is something else it will report an error saying that it can not be referenced from a type expression
Other languages
Type script doesn't allow this but they have an operator called typeOf that allows you to do this
Beta Was this translation helpful? Give feedback.
All reactions