首页 > 解决方案 > 具有推断类型的函数参数在 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.

  1. 为什么会这样?

当我输入xyof时,智能感知的问题停止了actions

  1. 如何在不输入xand的情况下使智能感知工作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;
    }
});

并且智能感知确实有效。

标签: typescriptvisual-studio-codetypescript-typingstypescript-generics

解决方案


推荐阅读