考虑
const darkPalette = [
'#255dbd',
'#2c6bd7',
'#5386e2',
'#7ea5e9',
'#49bbdb',
'#56d6f9',
'#89e2fa',
'#aaeafc',
'#00a690',
'#10bda4',
'#6ad8c8',
'#9de4da',
'#9dc53b',
'#bae050',
'#dcf0a3',
'#eaf6c8',
]
const transposePalette = compose(flatten, transpose, splitEvery(4))
const transposedDarkPalette = transposePalette(darkPalette)
当我收到transposedDarkPalette
的结果时,编译器会抱怨:
Types of property 'color' are incompatible.
Type '{}[]' is not assignable to type 'string[]'.
Type '{}' is not assignable to type 'string'
我可以通过
解决此问题const transposedDarkPalette = (transposePalette(darkPalette) as unknown) as string[]
但这有点丑陋,我想知道是否有更好的方法可以做到这一点。
答案 0 :(得分:1)
正如评论者已经指出的那样,TS的类型推断不能很好地通过compose
引入正确的类型。
一种解决方案是手动指定compose
的泛型:
const transposePalette = compose<string[], unknown, unknown, string[]>(flatten, transpose, splitEvery(4))
第一个通用属性是输入参数,最后一个通用属性是输出参数。为了使声明简短,将省略临时类型。