简体   繁体   English

Intellisense 显示'typeof import "/path"' - 如何获取导入变量的智能感知?

[英]Intellisense shows 'typeof import "/path"' - how to get intellisense of imported variables?

When I import some data and hover over the variable, it shows 'typeof import "/some/path"' instead of a list of the variables.当我在变量上导入一些数据和 hover 时,它显示'typeof import "/some/path"'而不是变量列表。

I would like it to show the list to enable autocomplete and such things.我希望它显示列表以启用自动完成和类似的东西。

I made an example project .我做了一个示例项目

If you hover over theme1 in index.ts of that project, it shows vars: typeof import("some/path");如果您在该项目的 index.ts 中index.ts over theme1 ,它会显示 vars: typeof import("some/path");

The way it happens is:它发生的方式是:

A folder has files with named exports.一个文件夹包含带有命名导出的文件。 There is an index.ts file which exports everything from that folder:有一个 index.ts 文件可以导出该文件夹中的所有内容:

//stuff/variables/index.ts
export * from '.';

. .

//stuff/variables/vars1.ts
const stuff1 = 'stuff1';
const stuff2 = 'stuff2';
const stuff3 = 'stuff3';

export { stuff1, stuff2, stuff3 };

A file in the parent folder imports all from that index.ts file, and exports it as a variable:父文件夹中的文件从该 index.ts 文件导入所有内容,并将其导出为变量:

//stuff/index.ts
import * as variablesData from './variables/index';

export const vars = variablesData;

Another file imports that variable and includes it in an exported object:另一个文件导入该变量并将其包含在导出的 object 中:

//stuff/theme.ts
import { vars } from './index';
    
const obj = {
  blue: 'blue',
};

export const theme1 = {
 ...obj,
 vars,
};

And lastly, the top index.ts imports that named export.最后,名为 export 的顶级 index.ts 导入。

//index.ts
import { theme1 } from './stuff/theme';
console.log(theme1);   

If I hover over that imported variable, it doesn't list the variables, but instead it shows typeof import("some/path");如果我 hover 在那个导入的变量上,它不会列出变量,而是显示typeof import("some/path");

So the export * from '.'所以export * from '.' is not resolved by intellisense.不是由智能感知解决的。 Can't I use `"export *"?我不能使用 `"export *" 吗?

Can this be helped by adding some type or writing it in a different way?这可以通过添加某种类型或以不同的方式编写来帮助吗?

I believe that what you are describing is a bug/missing feature in Typescript itself.我相信您所描述的是 Typescript 本身的错误/缺失功能。 Though I did not find it among github issues upon doing an ultra quick search there.虽然我在那里进行超快速搜索后没有在 github 问题中找到它。

What you can do is define the vars variable directly in vars1.ts file.您可以做的是直接在vars1.ts文件中定义vars变量。

//stuff/variables/vars1.ts
const stuff1 = 'stuff1';
const stuff2 = 'stuff2';
const stuff3 = 'stuff3';

const vars = { stuff1, stuff2, stuff3 };

export { stuff1, stuff2, stuff3, vars };

Then adjust your index file然后调整你的索引文件

//stuff/index.ts
export { vars } from './variables/index';

and your intellisense will work beautifully.你的智能感知会很好地工作。

Yes this approach is a little bit more work than you did, but at least the types work now:).是的,这种方法比您做的要多一些工作,但至少这些类型现在可以工作了:)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在VSCode中,如何通过IntelliSense导入React? - In VSCode, how to import React through IntelliSense? 如何在智能感知中获取 StyledComponent 的类型? - How to get StyledComponent's type in intellisense? 使用导入的第3方JS模块编写Typescript时,如何获得智能感知以使用VS Code? - How do I get intellisense to work with VS Code when writing Typescript with imported 3rd party JS modules? 如何使 vscode 导入智能感知与深度依赖关系 - How to make vscode import intellisense work with deep dependency 如何使用Intellisense将节点模块导入TypeScript? - How do I import node modules into TypeScript with Intellisense? VSCode IntelliSense:如何解析 SCSS 模块导入中的路径别名 - VSCode IntelliSense: How to resolve path aliases in SCSS module imports 在VScode编辑器中显示错误的Intellisense for Javascript - In VScode editor shows wrong Intellisense for Javascript TypeScript泛型以智能感知显示但无法编译 - TypeScript generics shows in intellisense but does not compile 如何在 React/TypeScript 应用程序中的模块方法上获取 VSCode 智能感知? - How to get VSCode intellisense on module methods in a React/TypeScript app? Typescript Vue 的 VSCode 智能感知(@Component 自动导入) - VSCode intellisense for Typescript Vue (Auto import at @Component)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM