-
Notifications
You must be signed in to change notification settings - Fork 93
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
"strictFunctionTypes" compiler option leads to error #39
Comments
cc @paduc |
I agree on the merit of the flag and error. I haven't had the time to reproduce and troubleshoot locally but I definitely will look into to it asap. |
Ok so I quickly looked through the official docs and noticed that stricter type checking applies to functions.
I changed the declaration to use methods and... it type checks! import { Result, Ok, Err } from "neverthrow";
export declare const errAsync: <T, E>(err: E) => ResultAsync<T, E>;
export declare class ResultAsync<T, E> {
private _promise;
constructor(res: Promise<Result<T, E>>);
static fromPromise<T, E>(promise: Promise<T>, errorFn?: (e: unknown) => E): ResultAsync<T, E>;
map<A>(f: (t: T) => A | Promise<A>): ResultAsync<A, E>;
mapErr<U>(f: (e: E) => U | Promise<U>): ResultAsync<T, U>;
andThen<U>(f: (t: T) => Ok<U, E> | Err<U, E> | ResultAsync<U, E>): ResultAsync<U, E>;
match<A>(ok: (t: T) => A, _err: (e: E) => A): Promise<A>;
then<A>(successCallback: (res: Result<T, E>) => A): Promise<A>;
}
const err1: ResultAsync<{}, { a: "a" }> = errAsync({ a: "a" }); // cat
const err2: ResultAsync<{}, { a: "a" } | { b: "b" }> = err1; // animal Could the type declarations be changed to methods for this class? I think this is related to contravariant method parameter types but I'm honestly not sure. I can't think of anything type unsafe someone could do with the current code. Anyway the codesandbox can be found here 😃 |
I don't think that's necessary. Thank you for creating the PR :) |
Thanks for the help @paduc! This issue will remain open even after #41 is merged because I want to grok the subtleties of this |
I agree, I don't think there is really much of a difference – just that there would be a lot more breaking changes if TypeScript removed method parameter bivarience. There is a proposal about making method parameters contravariant but it's four years old! Using my simple example from #36: export declare class ResultAsync<T, E> {
andThen: <U>(f: (t: T) => Ok<U, E>) => ResultAsync<U, E>;
}
const err1: ResultAsync<{}, { a: "a" }> = errAsync({ a: "a" });
const err2: ResultAsync<{}, { a: "a" } | { b: "b" }> = err1;
|
This is a continuation from this comment from #36 (comment)
Looks like the
strictFunctionTypes
compiler option leads to a rather cumbersome type error which looks to have merit.This SO thread and the official docs on the subject are great resources for explaining what
strictFunctionTypes
is, but I personally have not sat down to really think this through to know what the implications forneverthrow
are.Below is a demonstration where there are type errors when the
strictFunctionTypes
is turned on.Code example: https://repl.it/repls/SparseSaltyPdf
Screenshot:
The text was updated successfully, but these errors were encountered: