A. *_*Lau 14 javascript typescript visual-studio-code typescript-typings
有对应声明文件的文件是否有可能在vscode中检测自己的类型?
其他文件可以获取类型,但不能获取文件本身。
注意任何
但这有正确的打字
npm initindex.js index.d.ts import.jsindex.js
async function test(input) {
return null
}
Run Code Online (Sandbox Code Playgroud)
index.d.ts
type input_type = {
name: string,
salary: number
}
export function test(input : input_type) : Promise<null>
Run Code Online (Sandbox Code Playgroud)
import.js
import * as t from './index'
t.test()
Run Code Online (Sandbox Code Playgroud)
当您将鼠标悬停在其中的test函数上时,index.js它不知道其参数类型。但在import.js它知道它。
我怎样才能让index.js test函数知道它自己的类型。
据我了解,VSCode 将无法推断类型,因为该类型不存在于 index.js 的范围内。但是,它被导入到 import.js 中。
我能想到的唯一解决方法是使用 JSDoc 帮助 VSCode 推断类型,即使这样我们也必须导入类型:
/**
* @typedef {import(".").input_type} input_type
* @param {input_type} input
*/
async function test(input) {
return null
}
Run Code Online (Sandbox Code Playgroud)
显示工作:
| 归档时间: |
|
| 查看次数: |
272 次 |
| 最近记录: |