While using the MS VisualCode editor, I am attempting to implement type checking in my Javascript code. I want to maintain the flexibility of Javascript while also benefiting from type checking interfaces and data structures.
Based on the vscode documentation, I created a globals.d.ts
type definition file that looks like this:
interface QueryObj {
input: string;
query: string;
}
declare namespace TrainingModel {
function preprocessQuery(qo: QueryObj): QueryObj
}
Next, I attempted to declare a function that utilizes the QueryObj type:
checkOneData (queryObj) {
queryObj.foo = 1
I was expecting to receive a warning indicating that queryObj.foo
should not be used, but I did not.
I also tried adding a JSDoc to describe the parameters:
/**
* @param {QueryObj} queryObj
* @returns {Promise<any>}
*/
async checkOneData (queryObj) {
However, the line * @param {QueryObj} queryObj
resulted in the error cannot find name 'QueryObj'
.
I am aware that the .d.ts
file is being processed, as incomplete sections do trigger errors.
So, apart from basic parameter types like string/number, I am curious if VSCode can be utilized to validate user-defined data structures similar to how interfaces work in TypeScript.
References:
https://code.visualstudio.com/docs/languages/javascript https://github.com/Microsoft/TypeScript/wiki/Type-Checking-JavaScript-Files https://www.typescriptlang.org/docs/handbook/declaration-files/by-example.html