You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
having a defined way to document shape / value of a generic type like string , number. This could be a static compile time feature or it can be dragged into runtime.
Use Cases
Avoiding mistakes while creating cache keys
Validators for URLs in form of string
Validators for range
Examples
functionurl(urlStr: string){// Test ur, if okay do nothing, else return error with examples}functionipAddress(ipString: string){// Test ur, if okay do nothing, else return error with examples}constendPoint: string&(url|ipAddress)="0.0.0.0"
Checklist
My suggestion meets these guidelines:
This wouldn't be a breaking change in existing TypeScript/JavaScript code
This wouldn't change the runtime behavior of existing JavaScript code
This could be implemented without emitting different JS based on the types of the expressions
This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
But if we'll check it statically, it will not be possible even to compile this code because the type number, returned by the random function, will be not assignable to the type number & (lessThanHalf).
So, we can just add predicate to the type of value after checking the value with it.
constlessThanHalf=(num: number)=>num<0.5constnum=Math.random()if(lessThanHalf(num)){// num is `number & (lessThanHalf)` here}else{// num is `number` here}
But I still have a lot of questions:
How about functions with different names, but which are doing the same?
consta=(num: number)=>trueconstb=(num: number)=>trueconstc=0if(a(c)){// Should this be valid?constd: number&(b)=c}
Should predicates be pure? Why?
Can we use only named functions or anonymous too?
If yes, how should the type of variable b look like?
consta=0if(((num: number)=>true)(a)){// What is the type of b?constb=a}
If no — don't you think that limiting the checks with named functions only will unnecessarily increase the size of the code?
Yes predicates should be pure, to make it possible to statically evaluate and expect same result for a input any time
For 5, I guess you are missing the main feature of typescript type inference, as along as validator b is pure, we can copy it anywhere. Pure also means, no closure.
And mainly the validator api should have a design strategy where we decide what's the signature of predicate should be, It should never be any function. Every api needs to have schema and should be predictable.
Everything that has schema should be statically assertable, We define conventions in our codebase that's documented in a cornered file
example :
cache keys that have defined scheme for it, we know it, but we do not enforce it.
Ranges, we choose a random range for our operation and start using it every where but we do not know how to enforce/ educate a new employee about the convention other than KTs for weeks
Benefits of this is much more, having a simple compiler that asserts lua scripts without having to exploit the code base with 100 different kinds of files
I can't tell from this proposal whether you're proposing that TypeScript evaluate ipAddress at compile time (which we're not going to do), or if you're proposing that TypeScript insert code into the JS emit to cause ipAddress to be evaluated at runtime, which we also wouldn't do.
I am proposing that literals can be evaluated at compile time Ryan Cavanaugh (@RyanCavanaugh) . Node mostly runs on api side and does lots of query/scripts which can be statically evaluated.
Activity
MartinJohns commentedon May 5, 2020
I'd say this is a duplicate of #202 (combined with type guards). The cases "url" and "ip address" could also be solved by #6579.
junaid1460 commentedon May 5, 2020
yes, but regex makes it harder to read, also this looks more generic can be applied to any type, regex could be kind of validator within
I mean
where range could give a curried function that can accept type 'number' which is 8001
junaid1460 commentedon May 5, 2020
This could be just compiler internal implementation for most of the use cases
awerlogus commentedon May 5, 2020
If we'll drag it into runtime, it will give us ability to create programs with undefined behavior
For example:
But if we'll check it statically, it will not be possible even to compile this code because the type
number, returned by therandomfunction, will be not assignable to the typenumber & (lessThanHalf).So, we can just add predicate to the type of value after checking the value with it.
But I still have a lot of questions:
Should predicates be pure? Why?
Can we use only named functions or anonymous too?
If yes, how should the type of variable
blook like?If no — don't you think that limiting the checks with named functions only will unnecessarily increase the size of the code?
What about generic predicates?
How to handle this case?
The type of
crefers the scope that is not accessible from the scope wherecis declaredIMHO: the approach proposed in this issue adds much more problems than it solves
junaid1460 commentedon May 6, 2020
junaid1460 commentedon May 6, 2020
type inference, as along as validator b is pure, we can copy it anywhere. Pure also means, no closure.junaid1460 commentedon May 6, 2020
And mainly the validator api should have a design strategy where we decide what's the signature of predicate should be, It should never be any function. Every api needs to have schema and should be predictable.
junaid1460 commentedon May 6, 2020
Everything that has schema should be statically assertable, We define conventions in our codebase that's documented in a cornered file
example :
RyanCavanaugh commentedon May 6, 2020
I can't tell from this proposal whether you're proposing that TypeScript evaluate
ipAddressat compile time (which we're not going to do), or if you're proposing that TypeScript insert code into the JS emit to causeipAddressto be evaluated at runtime, which we also wouldn't do.junaid1460 commentedon May 7, 2020
I am proposing that literals can be evaluated at compile time Ryan Cavanaugh (@RyanCavanaugh) . Node mostly runs on api side and does lots of query/scripts which can be statically evaluated.