Comments (5)
I agree with you in general that we should try to make the type checking as strict as feasible. Your proposal for the pair
function is fine. The insert
function is less obvious. The example function of Ramda fails in case of the more strict checking:
R.insert(2, 'x', [1,2,3,4]); //=> [1,2,'x',3,4]
Typescript can not infer type T from the definition as string
conflicts with the list of number
s. It is a bit of an awkward example as it mainly shows that you can do weird things with javascript. So I would be in favor of the more strict approach and 1) correct the examples in the test file and 2) except that there will be some clashes in user's code where the 'freedom' of javascript is exploit which is in conflict with the more strict typing.
To be honest, up to now I haven't made a clear choice, but maybe now it is a good moment to do so. Any more thoughts/ideas on this issue?
from npm-ramda.
Regarding the "insert" example:
I absolutely agree that it is actually just an awkward thing allowed by javascript. What I am trying to say is that, I think it is better to make this awkwardness explicit, and have the strictly typed versions on the first place. So in your example it will look like this:
R.insert<any>(2, 'x', [1,3,4]);
Which means that if somebody needs awkwardness, no problem, just use "any" as the type explicitly. We can also have an overloaded versions with "any" for the cases where they will not override the strict typings.
Also, we can make this strict-to-loose typings even more gradual For example, additionally there could be a partly strict version with typed input but "any" output
insert<In, Out>(index: number, elt: In, list: Out[]): Out[];
//used like this
insert<string, any>(2, 'x', [1,2,3,4])
In this awkward case we will have at least a type-checked input, which is anyway better then nothing.
from npm-ramda.
@yazla I believe TS can't express an "array of single generic type". That's because JS has no tuples and you can't disallow array of mixed types by default because it will disable tuples.
from npm-ramda.
One more point regarding map, which is very important I think.
These definition:
map<T, U>(fn: (x: T) => U, obj: any): any; // used in functors
Is not that type safe for functors as it might be.
I believe it should be something like this:
map<T, U>(fn: (x: T) => U, obj: Functor<T>): Functor<U>;
I understand that defining Functors is out of Ramda's scope, but we can simply have a small interface with map method just for convenience(taking advantage of structural subtyping).
Basically my point is that if the function is supposed to work with functors, lets define that explicitly, not hiding that behind "any". We can still have "any" though, for just in case.
from npm-ramda.
@yazla: I've gone over all the typings again, and tried to strengthen inference using generics wherever possible, including for all noted examples here. If you have any more suggestions, they'd be welcomed, whether here or as PRs. Thanks for helping to make things better!
from npm-ramda.
Related Issues (20)
- npm-ramda motivation HOT 4
- R.Path - Typescript error for an array of objects
- `prop` does not work with optional keys HOT 1
- curry type
- `pipeWith` is missing
- Using groupBy with path always fails typecheck
- mergeDeepRight returns incompatible if objects contain array props HOT 1
- Add dissoc(prop: number)
- `transduce` and `into` are declared with `any` and for narrow use cases in general
- R.evolve is not deriving type from curried evolver
- Generics lost on currying for higher-order functions HOT 1
- type into in typescript
- How should we type lift?
- How to type pathOr?
- Expected 0 arguments, but got 1 HOT 3
- Evolve typings
- Typescript 4: Solve a lot of our problems
- Ramda 0.27 HOT 1
- Arity limitation for R.compose's rightmost function?
- Adding support for type guards to `partition` function.
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from npm-ramda.