Have enabled TypeScript checking in a JavaScript-only project in VS Code using the global setting
"javascript.implicitProjectConfig.checkJs": true
The type checking works, but for every external/global variable it shows a 'cannot find name' error:
$('body').addClass('something'); // cannot find name '$'
MyCustomObject.doSomething(); // cannot find name 'MyCustomObject'
How can I make VS Code recognize these global variables ? I think that this needs to be done using a custom definition file specified in a jsconfig.json, but haven't been able to make it work.
UPDATE:
Not a duplicate of Enabling implicit type checking for Javascript in VS Code results in "Cannot find name 'require'" error .
Have seen that ticket and it is related only to a node.js project and the accepted answer is related to installing the type definition npm package for nodejs. Which is not the case here. I need to set up a custom defintion file
You can declare global types as shown in https://www.typescriptlang.org/docs/handbook/declaration-files/templates/global-modifying-module-d-ts.html
Keep the declared types as any
so that tsc doesn't complain about not being a function or not having properties.
Create an index.d.ts file in the root folder with content
declare global {
var $: any;
var MyCustomObject: any;
}
export {};
After changing the index.d.ts file restart VSCode to make it recognize the types.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.