I have a new Javascript environment rather than Node.js or Browser and it has a new global object GameGlobal
, acting like global
in Node.js or window
in Browser. So if I assign a property to GameGlobal
, the property can be used in any module. Unfortunately, VSCode cannot recognize it.
/// <reference path="../../index.d.ts" />
//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#Import_an_entire_modules_contents
//import * as THREE from './js/three.module' //export {...}; cannot be recognized by the WeXin Developer Tool
//import * as THREE from 'three.min'
//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#Import_a_module_for_its_side_effects_only
//if ('object' == typeof GameGlobal) GameGlobal.initAndStart = initAndStart;
//if ('object' == typeof module) {
//module.exports = initAndStart; //<=> export default function initAndStart() {}
//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/var#var_hoisting
GameGlobal.THREE = require('three'); // <=> import * as THREE from 'three'
//import './SubdivisionModifier.js'
/** @type {typeof import('./TransformControls.js').TransformControls} */
GameGlobal.TransformControls = require('TransformControls').TransformControls; // <=> import { TransformControls } from './TransformControls.js'
//}
/// <reference path="../../node_modules/minigame-api-typings/index.d.ts" />
//https://github.com/wechat-miniprogram/minigame-api-typings
//install type definitions (DefinitelyTyped): npm install minigame-api-typings
//https://vscode-docs1.readthedocs.io/en/stable/languages/javascript/#defining-global-variables-outside-dts
/* global GameGlobal */
GameGlobal.mexport = function () { }
//https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#Import_a_module_for_its_side_effects_only
//import './js/sdk.js': Import an entire module for side effects only, without importing anything.
//This runs the module's global code, but doesn't actually import any values.
//import THREE from "three"
/** @param {THREE.Intersection} intersection*/
function onclick(intersection) {
intersection.object;
}
In the above example, even if GameGlobal.THREE = require('three')
, but the tooltip on both THREE
and Intersection
in @param {THREE.Intersection}
showed me "any".
Also, I referred to Defining Global Variables Outside .d.ts, but it didn't work.
IntelliSense Support
VS Code provides IntelliSense for built-in symbols of browsers, Node.js, and virtually all other environments through the use of type definition
.d.ts
files. DefinitelyTyped is a repository of typings files for all major JavaScript libraries and environments. The typings are easily managed using TSD, the TypeScript Definition manager. IntelliSense is automatically provided for CommonJS and AMD modules inside your project folders.
So, is there any way that I can customize and add such a global namespace object GameGlobal
for VS Code IntelliSense? I believe the key is probably to provide a .d.ts
file.
Node.js v13.9.0 Documentation: Global Objects
These objects are available in all modules.
The objects listed here are specific to Node.js. There are built-in objects that are part of the JavaScript language itself, which are also globally accessible.
global
Added in: v0.1.27
The global namespace object.
In browsers, the top-level scope is the global scope. This means that within the browser
var something
will define a new global variable. In Node.js this is different. The top-> level scope is not the global scope;var something
inside a Node.js module will be local to that module.
MDN: Standard built-in objects
The term "global objects" (or standard built-in objects) here is not to be confused with the global object. Here, "global objects" refer to objects in the global scope.
The global object itself can be accessed using the this operator in the global scope. In fact, the global scope consists of the properties of the global object, including inherited properties, if any.
- minigame-api-typings