【发布时间】:2019-07-10 05:10:28
【问题描述】:
我一直在使用 Typescript 中的一些通用代数数据类型(v3.5.2 应该是最新的),它似乎无法检测似乎应该能够弄清楚的函数的签名匹配:
type Success<T> = {
tag: 'success';
value: T;
fbind<R, E>(func: (value: T) => Result<R, E>) : Result<R, E>;
}
type Failure<E> = {
tag: 'failure';
error: E;
fbind<T>(_func: (value: T) => Failure<E>) : Failure<E>;
}
type Result<T, E> = Success<T> | Failure<E>;
function success<T>(value: T) : Success<T> {
return {
tag: 'success',
value,
fbind<R, E>(func: (value: T) => Result<R, E>): Result<R, E> {
return func(this.value);
},
};
}
function failure<E>(error: E) : Failure<E> {
return {
tag: 'failure',
error,
fbind<T>(_func: (value: T) => Failure<E>) : Failure<E> {
return this;
},
};
}
type ValidationError = string;
function parseDate(value: string) : Result<Date, ValidationError> {
// simple example to show issue
return success(new Date(value));
}
function logDate(date: Date) : Result<Date, ValidationError> {
console.log('Date: ', date);
return success(date);
}
parseDate('1-1-2019').fbind(logDate);
在.fbind 调用logDate 时,它输出以下错误:
Cannot invoke an expression whose type lacks a call signature. Type '(<R, E>(func: (value: Date) => Result<R, E>) => Result<R, E>) | (<T, R>(_func: (value: T) => Result<R, string>) => Result<R, string>)' has no compatible call signatures.
是否有我没有看到的错误?或者,有没有办法为类型系统提供一些帮助,以便它能够弄清楚?
更新
使用以下代码:
type Success<T> = {
tag: 'success';
value: T;
fbind<R, E>(func: (value: T) => Result<R, E>) : Result<R, E>;
}
type Failure<E> = {
tag: 'failure';
error: E;
fbind<T, R>(_func: (value: T) => Result<R, E>) : Result<R, E>;
}
type Result<T, E> = Success<T> | Failure<E>;
function success<T>(value: T) : Success<T> {
return {
tag: 'success',
value,
fbind<R, E>(func: (value: T) => Result<R, E>): Result<R, E> {
return func(this.value);
},
};
}
function failure<E>(error: E) : Failure<E> {
return {
tag: 'failure',
error,
fbind<T, R>(_func: (value: T) => Result<R, E>) : Result<R, E> {
return this;
},
};
}
type ValidationError = string;
function parseDate(value: string) : Result<Date, ValidationError> {
// simple example to show issue
return success(new Date())
}
function logDate(date: Date) : Result<Date, ValidationError> {
console.log('Date: ', date);
return success(date);
}
parseDate('1-1-2019').fbind(logDate);
在.fbind 调用logDate 时,仍然输出以下错误:
Cannot invoke an expression whose type lacks a call signature. Type '(<R, E>(func: (value: Date) => Result<R, E>) => Result<R, E>) | (<T, R>(_func: (value: T) => Result<R, string>) => Result<R, string>)' has no compatible call signatures.
在 typescript repo 上提出问题后,typescript 目前似乎不支持在 3.5.3 及更低版本 (https://github.com/microsoft/TypeScript/issues/32314) 中调用多个通用函数签名的联合。
【问题讨论】:
标签: typescript