如何使函数参数成为通用对象,在 Typescript 中使用一种类型或 void 的两个函数
How to make function argument that is generic object consuming two functions with one type or void in Typescript
我在打字稿中遇到以下情况:
type Matcher<T, U> = {
First: (arg: T) => U,
Second: () => U
};
class Main<T> {
constructor(private value: T) {
}
match<U>(matcher: Matcher<T, U>): U {
return this.value
? matcher.First(this.value)
: matcher.Second();
}
}
const main = new Main(10);
const res = main.match({ // there is a problem
First: v => v + 10,
Second: () => console.log()
});
所以我有一个对象,用户必须将其传递给 class 实例的 match
方法。该对象应包含两个函数:First
和 Second
。这函数 returns 一种类型的值(例如 number
)或一种类型的值 + void
(例如 number
+ void
),但没有别的。不能有 string
+ number
类型。
此代码因错误而失败
The type argument for type parameter 'U' cannot be inferred from the usage. Consider specifying the type arguments explicitly.
Type argument candidat 'void' is not a valid type argument because it is not a supertype of candidate 'number'.
我明白为什么会出现这个错误(U
是单一类型,但是函数有两个不同的类型,它们不能合并等等),但是我该如何解决这个问题?我需要:
- 严格输入,所以不应该有
any
类型
- 允许两个函数只使用一种类型,或者
void
在一个或两个函数中使用。不允许 number
和 string
作为返回类型。
是否可以使用 typescript 类型系统?
您可以使用 union types:
type Matcher<T, U> = {
First: (arg: T) => U;
Second: () => U | void
};
我只在第二个函数中添加了 void
,但您也可以在第一个函数中使用它。
但是您还需要 match
方法来 return | void
:
match<U>(matcher: Matcher<T, U>): U | void {
return this.value
? matcher.First(this.value)
: matcher.Second();
}
编辑
如果我理解正确,那么这可能会有所帮助:
type Matcher<T, U> = {
First: (arg: T) => U;
Second: () => U;
};
type MatcherOne<T, U> = {
First: (arg: T) => void;
Second: () => U;
};
type MatcherTwo<T, U> = {
First: (arg: T) => U;
Second: () => void;
};
class Main<T> {
constructor(private value: T) { }
match<U>(matcher: Matcher<T, U>): U;
match<U>(matcher: MatcherOne<T, U>): U | void;
match<U>(matcher: MatcherTwo<T, U>): U | void;
match<U>(matcher: Matcher<T, U> | MatcherOne<T, U> | MatcherTwo<T, U>): U | void {
return this.value
? matcher.First(this.value)
: matcher.Second();
}
}
我在打字稿中遇到以下情况:
type Matcher<T, U> = {
First: (arg: T) => U,
Second: () => U
};
class Main<T> {
constructor(private value: T) {
}
match<U>(matcher: Matcher<T, U>): U {
return this.value
? matcher.First(this.value)
: matcher.Second();
}
}
const main = new Main(10);
const res = main.match({ // there is a problem
First: v => v + 10,
Second: () => console.log()
});
所以我有一个对象,用户必须将其传递给 class 实例的 match
方法。该对象应包含两个函数:First
和 Second
。这函数 returns 一种类型的值(例如 number
)或一种类型的值 + void
(例如 number
+ void
),但没有别的。不能有 string
+ number
类型。
此代码因错误而失败
The type argument for type parameter 'U' cannot be inferred from the usage. Consider specifying the type arguments explicitly.
Type argument candidat 'void' is not a valid type argument because it is not a supertype of candidate 'number'.
我明白为什么会出现这个错误(U
是单一类型,但是函数有两个不同的类型,它们不能合并等等),但是我该如何解决这个问题?我需要:
- 严格输入,所以不应该有
any
类型 - 允许两个函数只使用一种类型,或者
void
在一个或两个函数中使用。不允许number
和string
作为返回类型。
是否可以使用 typescript 类型系统?
您可以使用 union types:
type Matcher<T, U> = {
First: (arg: T) => U;
Second: () => U | void
};
我只在第二个函数中添加了 void
,但您也可以在第一个函数中使用它。
但是您还需要 match
方法来 return | void
:
match<U>(matcher: Matcher<T, U>): U | void {
return this.value
? matcher.First(this.value)
: matcher.Second();
}
编辑
如果我理解正确,那么这可能会有所帮助:
type Matcher<T, U> = {
First: (arg: T) => U;
Second: () => U;
};
type MatcherOne<T, U> = {
First: (arg: T) => void;
Second: () => U;
};
type MatcherTwo<T, U> = {
First: (arg: T) => U;
Second: () => void;
};
class Main<T> {
constructor(private value: T) { }
match<U>(matcher: Matcher<T, U>): U;
match<U>(matcher: MatcherOne<T, U>): U | void;
match<U>(matcher: MatcherTwo<T, U>): U | void;
match<U>(matcher: Matcher<T, U> | MatcherOne<T, U> | MatcherTwo<T, U>): U | void {
return this.value
? matcher.First(this.value)
: matcher.Second();
}
}