typescript - 具有推断类型的函数参数在 return 中没有智能感知,而在函数体的其余部分有
问题描述
这是发生此问题的代码:
function component<T>(p: {
actions : () => T,
htmlTemplate: <S extends T>(actions : S) => any
}) {
return p;
}
component({
actions : () => ({
a : (x,y) => x+y
}),
htmlTemplate : (actions) => {
/*try to dot into the actions and you will get intellisense*/
actions;
/*but if you dot here you will get no intellisense*/
return actions;
/*
intellisense appears in the `return actions` If I type `x` and `y`
how can I have no problem with intellisense wihtout providing types for `x` and `y`?
*/
}
})
在htmlTemplate
函数体中,actions
参数有智能感知,但在return
.
- 为什么会这样?
当我输入x
和y
of时,智能感知的问题停止了actions
。
- 如何在不输入
x
and的情况下使智能感知工作y
?
我正在使用 vscode 并且我的项目没有tsconfig.json
文件。
编辑:一个丑陋的“解决方案”是这样做:
const component = <T>(p1: {
actions: () => T
}) => (p2: {
htmlTemplate: <S extends T>(actions: S) => any
}) => Object.assign({}, p1, p2)
component({
actions: () => ({
a: (x, y) => x + y
})
})({
htmlTemplate: (actions) => {
actions;
return actions;
}
});
并且智能感知确实有效。
解决方案
推荐阅读
- wordpress - 我无法在实时服务器上上传我的本地主机网站。它没有显示背景图像
- c# - Entity Framework Core:读取和删除数据之间的事务
- apache-spark - Spark-kafka:从 Spark 写入流时出现 org.apache.kafka.common.errors.TimeoutException
- angular - 限制刷新页面|浏览器限制| 角 6 |
- javascript - 函数内的变量范围
- c# - 在写入文件期间增加 ID(C#、CSV)
- angular - 为什么在 Angular 中使用 Gulp
- c# - SSIS 参考错误 Oracle DataAccess - 使用自定义类型参数调用 PL/SQL 过程
- c++ - std::list 可以用于简单的无锁队列吗?
- php - mysql中的WP序列化查询