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
refine types: arrays may be readonly #877
Conversation
You've replaced mutable arrays with read-only arrays. But while read-only arrays are subset of generic arrays, generic arrays do not inherit from read-only arrays. Right now we have a general case. You've reducing this to read-only-arrays only. In fact, on two lines you're explicitly casting types using |
Just to be sure we're talking about the same thing: "generic arrays" and "mutable arrays" are the same, right? Since readonly arrays are generic arrays (the simple The current solution is a general case, but could be even more generic, specifing in the type-definition, that the I agree that using |
So what i’m trying to understand here is Typescript type hierarchy. ReadOnlyArray inherits from Array Which means we will receive type errors when compiling this code: watcher.add([“path”]). Right? |
I see. Well typescript does not use type hierarchy. What is relevant is what I briefly mentioned as "structural typing". TSC sees a type-requirement and checks if the argument fullfills the required interface. Thus
is not right. I put together a simple example to further demonstrate that. I also updated the pull-request to be more verbose and show that really generic arrays still work. :) |
Weird. I can’t reopen the PR. Could you create a new one? |
Sure, thanks for taking the time to discuss this issue, I really appreciate it :) |
Hey,
I started using this fine module in a typescript project with lint-rules that enforce the usage of readonly-arrays. Chokidar's types expects mutable arrays, and checking the source-code readonly-arrays should be totally ok, so it's only the types that are too broad.