-
Notifications
You must be signed in to change notification settings - Fork 2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Type Definitions for TypeScript #272
Comments
? |
|
Can you give a specific example of a use case for this library? |
It's impossible to consume your library from a TypeScript project without type declarations. // today, typescript shows an error here: Could not find a declaration file for 'cpsfy'. 'cpsfy' implicitly has an 'any' type. (TS7016)
import { pipeline } from "cpsfy"
const foo: string = "fizz \n \tbuzz"
// typescript should be able to infer 'bar' is `string[]` using cpsfy type declarations
// today, it cannot
const bar = pipeline(foo)(
line => line.split("\n"),
words => words.map(word => word.trim())
) |
From your code, I understand you are using the Your use of the By 'impossible to consume', do you mean Typescript is not able to handle plain JS modules without errors? What about the |
Ramda has a DefinitelyTyped package: https://www.npmjs.com/package/@types/ramda i.e. type definitions are provided, just not directly. You can consume plain JS from typescript without errors. But you lose out on all the benefits of using TypeScript in the first place, because your type-safety goes out the window. |
Ok, an array of strings :) I see these are a separate package and a repository for Ramda. |
the readme states intention to move the type declarations:
Basically, |
My intent is to keep this repo as light and tiny as possible with zero dependency, which I see as one of the attractive features. I would be worried about adding any complexity and especially dependency packages from npm that unfortunately tend to deteriorate over time and make installation a nightmare with lots of warnings. Even now I am getting massive numbers of warnings just from the testing packages that used to be absent in the past. If those additions could be done separately, I would prefer it, especially since I don't use TS myself. |
You don't need any dependencies. Just a For checking the syntax of the file you can optionally install The |
The additional TS package, not the smallest one which is not really related to the project, would be a dealbreaker for me, it is against the minimalistic philosophy to keep things to a bare minimum. Given that the same feature can be achieved in an independent repository like for Ramda, which can be maintained separately in a fully decoupled fashion, e.g. only for certain operators rather than for all, I would prefer that solution here as well. |
Having a separate package is not minimalist, it's maximalist. It creates more work for everyone. An API change here, requires communication to maintainers over there, who have to make a PR at https://github.com/DefinitelyTyped/DefinitelyTyped, and then consumers have to remember to bump 'Not the smallest one' - yes, the TypeScript package is 21MB ... with no dependencies.
You don't need to install
'Not really related to the project' - arguably, neither are |
At this stage, this library is mature and changes are rare. So it wouldn't be much work for me to ping the other maintainers. Or they can subscribe to changes and get notifications when new versions come out. Or even automate things via Github actions. On the other hand, if this additional feature is kept locally, any change to the core library will add more work for me to take care of updating the TS support, which I am not too familiar with. Other contributors may be busy at the time, and so the library's quality will decline. Or else, additional tooling and automation would have to be written and maintained, adding more complexity. This is the real issue. Plus, there would have to be some automation tools to check the quality of the types, where, as I said I have no experience. Also, having a separate repository will give more freedom and empower other contributors not to wait for their PR approval when adding tools as they like. Ramda had not made that anticipated change yet, and no timeline is given, despite their massive number of collaborators. I can only guess they have some serious reason like that change may not be trivial and could be a lot of work. It will likely have to start with one or two operators that people actually use in their TS project and that matter to them. Your example contains Would you know how to write those type declarations for the more advanced operators like Let us look at the simple identity function As for the dependencies, indeed, with Otherwise, I'd be more than happy to remove them :) |
I see no reason they wouldn't.
I have some experience with this, yes - it'll be a bit tricky and require use of type parameters (generics). This file contains declarations for a similar-ish API to
In typescript, the code const f = x => x will implicitly type const x = "foobar" // x has inferred type `string`
const y = f(x) // y has inferred type `any` - it could be a number, or an object, etc. as far as typescript is concerned We could annotate const f = (x: string) => x where Or we could annotate the return-type explicitly: const f = (x: string): string => x To retain the information that the type returned will be in some way related to the type of the provided parameter, we would have to use a generic type parameter: const f = <T>(x: T): T => x the const x = "foobar" // x has inferred type `string`
const a = f<string>(x) // a has inferred type `string`
const b = f(x) // the generic parameter `T` is inferred to be `string` because `x` is `string`. b has inferred type `string`
const y = 123
const c = f(y) // c has inferred type `number` |
The problem with AVA and tape tests is that the tests are loading those runners as modules, which doesn't seem to work with I like the declarations const f = <T>(x: T): T => x as they would help to understand the operators. For example, the vector identity function f = (...x) => [...x]and It should take tuples to tuples but since we don't have tuples type in JS, just use the array but the meaning should be the same. Now the correct type annotation might be That same problem appears for every other variadic operator here, of which there are most. |
Re. Ava and tape: ah, I see - I think you're right. I forgot that the test-runners contained import-able utilities. There might be a very hacky way of doing it whereby you add the path containing temporarily downloaded packages to the list of package resolving directories, but it would hardly be worth the time. Re. The vector identity, it can be annotated like this: const f = <T extends unknown[]>(...args: T): T Tuples in JS are Usage (homogeneous array): const a = f<string[]>("foo", "bar", "baz") Usage (heterogeneous tuple): const a = f<[string, number]>("foo", 10) |
In fact, I see the AVA readme says it explicitly: Make sure to install AVA locally. AVA cannot be run globally. No motivation or reasoning is provided. Perhaps it is their advertised intent to avoid globals and get everything from their local module instead. These days, jest seems to be the most popular, which does a limited set of globals, which doesn't seem like a major problem. And best of all, it does seem to work via npx, so I am tempted to switch to it, away from ava & tape. Comparing expect(sum(1, 2)).toBe(3)
//vs
t.is(sum(1,2), 3) I still prefer the simpler ava syntax achieving the same result with only one method call instead of 2. About the annotations, I'd like to see how it comes out in practice on a small scale, so if you like to pick one operator that matters most to you and write a minimum possible PR for that operator aiming to achieve the goal you anticipate? Maybe a separate folder with all TS related files? Additional quality checking npm scripts are fine too, including npx, just no forced package installation please. Another thing to keep in mind, the addition should not scare away people who don't use TS, so the original code should stay the same but I don't mind to add comments next to the function definitions and in the documentation. |
My personal preference these days is The design of the
Would you like me to PR this repository or PR the |
I just opened the new issue I have no experience with vitest but if you have any thoughts about its advantages over jest, you are welcome to add your comments. Also if you see any advantages of the As for PR, I would say whatever takes less time to get started and will potentially be simpler and less time-consuming in the future (which simpler things usually are :-) |
A relevant PR in the tape repo tape-testing/tape#603 |
No description provided.
The text was updated successfully, but these errors were encountered: