|
|
|
|
|
by bern4444
2202 days ago
|
|
I love this idea too. Is it possible to create a type (in F# or Typescript) to represent an idea like greaterThan2? A value whose type is greaterThan2 would have the obvious constraint the value is always bigger than 2. Having the compiler check such a condition would be awesome. I kind of can do it for Strings by doing something like this type ValidStrings = 'name' | age | 'dob';
The easy way around this is a function to determine this function greaterThan2(num) return num > 2;
but it'd be cool to express this level of dynamism as a type. |
|
Smart constructor technique works well with 'parse, don't validate' approach [0]. You can push type construction to the boundries of your system so that you can work on a domain code with more precise types. It's not always so rosy however as too much types can become a burden.
[0] https://lexi-lambda.github.io/blog/2019/11/05/parse-don-t-va...