Can I use custom type definitions in an ES6 project?

My team is working on a relatively large NodeJS project written in ES6, crammed with babel and then deployed as AWS lambdas with Serverless. This project is focused on consumption, display / conversion and output of one specific type of object that we have identified.

Our problem is that ECMA / JavaScript is not strongly typed, so if we make a mistake, for example, treating a field as an array somewhere and a string somewhere else, there is nothing to catch except runtime errors. We also poorly documented the structure of this object, so sometimes consumers send us instances of the object with data in the fields with a small number of names, which we say, we process, but actually do not use it.

I’m looking for a way to create some kind of schema or type definition for this particular object in our project, so we can use this to fix our code, make our processing more reliable and create much better documentation for it. Now I know that VSCode offers some basic type checking in JavaScript, but I don’t think you can try the JSDoc really large object, and then put this document in every file that uses this object. I found that VSCode can also somehow control the validation with .d.ts files , but I don’t understand whether and how I can use what for the specific, custom object that we developed. Most of what I found seems to be related to pulling .d.ts files for external libraries.

So TL: DR. Is it possible, in a NodeJS / ES6 project, to make one object widely used in this project strictly typed? Error checking in VSCode would be acceptable, but some kind of command line combination that we could run before the transcription would also be great.

+7
javascript ecmascript-6 typechecking visual-studio-code
source share
3 answers

Ok, got it. After I posted this question, I continued to search the Internet and an hour or so later got into this article on StrongLoop by Sequoia McDowell: https://strongloop.com/strongblog/type-hinting-in-javascript/

I watched him closely and, using the "typings" package, I managed to initialize the "typings" folder in the root of my project. The contents of this folder now look like this:

typings/ β”œβ”€β”€ models/ β”‚ └── myObject.d.ts └── index.d.ts 

The contents of this index.d.ts file are as follows:

 /// <reference path="models/myObject.d.ts" /> 

And the contents of this myObject.d.ts file look something like this:

 declare namespace MyProject { export interface MyObject { aString?: string; anotherString?: string; aComplexType?: ComplexType; } interface ComplexType { aString?: string; anotherComplexType: SecondComplexType; } interface SecondComplexType { aString?: string; } } 

With this, I had to start tagging instances of this object using JSDoc. This document basically took two forms:

 /** * Creates an instance of UtilityThing. * @param {MyProject.MyObject} myObject * * @memberof UtilityThing */ constructor(myObject) { this.myObject = myObject; } /** * @param {MyProject.MyObject} myObject * @returns {MyProject.MyObject} */ function noOp(myObject) { return myObject; } 

and

 /** @type {MyProject.MyObject} */ const myObject = containerObject.myObject; 

With this installation and the latest public version of VSCode, I was able to see errors in the ES6 * .js files that I was currently editing, and said which attributes do not exist, which were assigned values ​​of the wrong type, which were considered the wrong type, etc.

On the halfway.

After some research, I realized that this is not a completely unique VSCode feature. It looks like they are using tslint or some kind of individual version. Working with this knowledge, I added "tslint" to the project and developed this npm script:

 "tslint": "tslint --type-check --project tsconfig.json" 

Here is the contents of tsconfig.json that I landed on, although I'm not entirely sure that all of these options are necessary.

 { "compilerOptions": { "target": "es6", "allowJs": true, "noResolve": true, "checkJs": true, "moduleResolution": "node", "types": [ "node" ] }, "exclude": [ "node_modules", "coverage", "lib", "spec" ] } 

Running this "tslint" script using this tsconfig.json, and the type definition files in the "typings" folder allow me to enter the type of one type of object in all project files with the proper JSDoc. I ran into a small problem , but it looks like it was fixed and merged about an hour ago, by coincidence. In addition to the type of checking the fields of the object, it also revealed a couple of places where the attribute was prematurely pulled from the object whose descendant did have this attribute. Very cool.

TL; DR: This can be done, thank you very much Sequoia McDowell for this article, which finally put me on the right track.

+4
source share

You cannot have a strong type in Javascript, and even if you can, you shouldn't, javascript was created for a dynamic language, but in typescript you can

Check out this link: https://basarat.gitbooks.io/typescript/docs/quick/nodejs.html

+1
source share

I found out that you can simply set // @ts-check at the beginning of JS files, and the *.d.ts file will be recognized (at least in the project root directory).

VS Code Version: 1.22

Edit:

So, I created jsconfig.json with this content:

 { "compilerOptions": { "jsx": "react", "checkJs": true, "noResolve": true, // because some imports didn't work "moduleResolution": "node" // because of npm i <git or local dependency> }, "include": ["src/**/*", "types/**/*"], "exclude": ["node_modules"] } 

And inside the types folder, I have some *.d.ts files.

Everything works fine, and you don't need to explicitly set // @ts-check .

0
source share

All Articles