不能将类型“void”分配给类型“T[]”
type MyFilter2 = { <T>(array: T[], f: (t: T) => boolean): T[]
}
const myFilter2: MyFilter2 = (array, f) => {
let result = []
for(let i =0;i<array.length;i++){
const item = array[i]
if(f(item)){
result.push(item)
}
}
}
myFilter2(names, (item) => item.name.startsWith('b'))
tsconfig.js
{ "compilerOptions": {
"lib": ["ES2015"],
"module": "CommonJS",
"outDir": "dist",
"sourceMap": true,
"strict": true,
"target": "ES2015"
},
"include": ["src"]
}
package.json
{ "devDependencies": {
"@types/node": "^16.7.4",
"tslint": "^6.1.3",
"typescript": "^4.4.2"
}
}
回答:
你这 filter 里啥也没返回啊,不应该在最后 return result
么?
以上是 不能将类型“void”分配给类型“T[]” 的全部内容, 来源链接: utcz.com/p/936367.html