在TypeScript中,有没有一种方法可以对一个函数进行建模,该函数可以返回已经指定的联合类型,就像下面例子中的myFunc?"two"
myFunc("a", "two") // ERROR: only allowed second argument should be "a1" or "b1" or "c1"
假设,如果我有以下函数: function nullOrString(): string | null {} 这不会产生任何错误: const value = nullOrStringnot assignable to type 'string'. if (nullOrString()) {} 是我误解了函数,还是这是一个TypeScript错误?
我正在尝试使用fp-ts和redux-observable来构建一些处理一些api请求的epics。我在使用时遇到了一个问题,如果我不将我的操作转换为AnyAction类型,我会得到一个类型错误,告诉我这两个类型应该是相同的。in type '{ user: User<Attributes>; }' but required in type '{ error: Error | null; }'
我也尝试过使用,因为它希望返回</