Is it possible for me to define TypeScript interfaces to be used in vanilla JavaScript projects within VSCode?

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

Answer №1

A simple solution is to create a new TypeScript project with the appropriate configuration file. You can easily find tutorials on Google or YouTube, such as this one: https://www.youtube.com/watch?v=dPgAXFcFHCM

Once you have set up your project, you can code in JavaScript within your TypeScript file. The TypeScript compiler will convert your code to vanilla JavaScript based on your project configuration. You can then use the compiled JavaScript file in any other project as needed.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Guide to preserving canvas state in React?

I am currently working on a drawing application that allows users to draw lines on a canvas. The functionality is such that the line starts drawing on the first click, continues as the mouse moves, and stops on the second click. However, each time a user ...

"Embedding PHP code within HTML tags, which is then embedded within

Running into an issue within a while loop... echo 'var contentString = '<div id="content" > <div id="bodyContent"> <p>' + $row[name]+ '</p> ...

A guide on how to efficiently update and submit a reactive form with a single click of the submit button in Angular

Currently, I have a view component setup where clicking the edit button directs me to the register component for form updates using patchvalue. The issue that I am facing is that when I update and register the form using the same button, it creates anothe ...

What are the possible reasons for a checkbox not being checked in React JS?

I've been working with react final form and I'm encountering an issue where I can't seem to get the checkbox to be properly checked. I'm not sure what mistake I might be making. Take a look at my code on CodeSandbox const AppWithIconTo ...

send JSON data to a Spring MVC endpoint

Here is the controller signature I have tried using @RequestBody: @RequestMapping(value = "/Lame", method = RequestMethod.POST) public @ResponseBody boolean getLame(@RequestParam String strToMatchA, @RequestParam String strToMatchB) {} This is the json I ...

What is the best method for implementing ajax in Codeigniter?

Exploring the best approach to writing ajax in CodeIgniter. Is it acceptable to write ajax in views, or is it better to create a new directory named assets at the root of the CodeIgniter project folder and relocate all scripts to a js folder within our a ...

Having trouble with Node.js executing commands in the console

I've been following some tutorials on YouTube to learn how to create a real-time chat using Node.js from the phpacademy channel. Currently, I'm stuck at the step where I need to run my server.js file in the console. When I enter the command ...

Prevent access to URLs that do not match a certain pattern in Chrome

I will be implementing a filter to block request URLs that do not match a specific pattern. To achieve this, you can utilize the request blocking feature within Google Chrome's network tab. Simply right click on the request Row and select "block requ ...

Constantly showing blank white pages on my website specifically in Internet Explorer 11

I successfully developed a website using react, babel, webpack, and backend Django framework. Everything runs smoothly on Chrome, Safari, and Firefox, but when it comes to IE11, issues arise. Initially, the site functions properly, but after navigating thr ...

Optimizing Google e2e testing using Protractor

Improving login efficiency is necessary to enhance the speed of executing e2e tests. At present, after every test, the Chrome browser shuts down, requiring a new login session for each subsequent test. What changes can be made to address this issue? Any ...

Having Trouble Loading Vue Devtools in Vue Electron Builder

I'm encountering an issue with loading Vue Devtools in my Electron application integrated with Vue. This is my first time working with this combination, and I suspect there might be a dependency problem causing the Devtools not to load within the Elec ...

What is the best way to retrieve variables from child components within a parent component in React?

I'm currently diving into React by working on a form/calculator application. I've come to realize that React follows a unidirectional pattern, and I'm struggling with how to deal with it. My goal is to create a table where users can input i ...

Navigating through Sails.Js: A Guide to Implementing Pagination

Looking to implement paginated tables in your application using sails.js, mongodb, and waterline-ORM? Wondering if there is a recommended method for pagination in sails.js? ...

typescriptIs it possible to disregard the static variable and ensure that it is correctly enforced

I have the following code snippet: export class X { static foo: { bar: number; }; } const bar = X.foo.bar Unfortunately, it appears that TypeScript doesn't properly detect if X.foo could potentially be undefined. Interestingly, TypeScript ...

Importing vs Referencing Videos in Next.js - What is the Best Practice for Video Integration in Next.js?

I'm looking to use a video as a background in my banner design. Typically, with images in Next.js, I would import them and then pass the import as src (for example: import img from '@images/about-us-page/img.svg'). However, when attempting ...

Issues arising from utilizing Twitter Bootstrap 3.1.x, the box-sizing property, and Revolution Slider

I'm currently working on a PyroCMS theme that is built with Twitter Bootstrap 3.1.x and includes Revolution Slider. However, I've encountered an issue where the property box-sizing: border-box; creates an unwanted grey border as shown in the imag ...

What are the steps to store my information in MongoDB with the help of Expressjs?

As a backend developer beginner, I am currently working with an array called Movie using expressJS. My goal is to save this array in a MongoDB database, specifically using Mongodb Atlas. Any help or guidance on this process would be greatly appreciated. I ...

send multiple textbox values to controller in CodeIgniter

I am new to Codeigniter and I'm facing some difficulties in understanding how to accomplish a task. In my view page, there are five rows generated using a for loop. Each row consists of two select boxes and two input boxes. I don't know how to re ...

The select2 option seems to be malfunctioning as it is not

I have implemented a dropdown using Select2. Although I am able to retrieve data from the backend and display it successfully in Select2, I'm facing an issue with certain data that contains multiple spaces between words. For example: Test&nbsp;& ...

Tips for initiating a function at a designated scroll point on a webpage

Currently, I am testing out a code snippet that allows images to flip through in a canvas element. I'm curious if it's possible to delay the image flipping effect until the viewer scrolls down to a specific section of the page where the canvas i ...