我安装的软件包是lodash
和@types/lodash
。
我有:
import _ from 'lodash';
function doSomething(): string[] {
const letters = ['c', 'a', 'b'];
return _.orderBy(letters, [null], ['asc']);
}
console.log(doSomething());
但是return _.orderBy(letters, [null], ['asc']);
抛出错误:
Type '(string | number | (<U>(callbackfn: (value: string, index: number, array: string[]) => U, thisArg?: any) => U[]) | (() => string) | (() => string) | (() => string | undefined) | ((...items: string[]) => number) | ... 25 more ... | { ...; })[]' is not assignable to type 'string[]'.
Type 'string | number | (<U>(callbackfn: (value: string, index: number, array: string[]) => U, thisArg?: any) => U[]) | (() => string) | (() => string) | (() => string | undefined) | ((...items: string[]) => number) | ... 25 more ... | { ...; }' is not assignable to type 'string'.
Type 'number' is not assignable to type 'string'.
但显然,将返回字符串数组。
除了强制铸造到之外,还有什么方法可以解决此问题return _.orderBy(letters, [null], ['asc']) as string[];
?有没有一种方法可以指定orderBy
要从其继承的类型letters
?
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句