Mixing JavaScript and TypeScript in Node.js

Having parts of Node.js in simple ES6, is it possible to mix in some Typescript modules inside the same project?

eg. with some types defined in Typescript that are imported through requireinto simple ES6 files?

+9
source share
1 answer

Yes it is possible.

Combine the following compiler flags

  1. --allowJs

    Explicitly supports mixed JavaScript and TypeScript sources

  2. --outDir

    Since all files will be transferred, it is necessary to output the received JavaScript to another directory, otherwise the input files .jswill be overwritten 1 .

  3. --checkJs

    . , JavaScript, , TypeScript, .

, TypeScript JavaScript, .

TypeScript JavaScript , Visual Studio Code.

JSDoc 2. , .ts TypeScript (.ts/.tsx/.d.ts). IDE, Visual Studio Code, .

. JavaScript , , . TypeScript, .

:

a.ts

export default createThing;

function createThing(...args): createThing.Thing  {...}

namespace createThing {
  export interface Thing {...}
}

b.js

import createThing from './a';

/**
 * @param {createThing.Thing} thing
 */
export function takesThing(thing) {}

:

1: --outDir , --noEmit. , SystemJS ( plugin-typescript) Webpack ( awesome-typescript-loader ts-loader) TypeScript. , TS Node.

2: JSDoc, TypeScript, JSDoc. , TypeScript Google Closure Compiler, JSDoc . , , , .

:

- JavaScript, , .

, JSDoc, TypeScript, JSDoc . --allowJs --allowJs , .

+14

Source: https://habr.com/ru/post/1695729/


All Articles