How to use 'js to ts converter online' in JavaScript

Every line of 'js to ts converter online' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
7function jsToTs(code) {
8 const output = code
9 // Change `+x` to `readonly x`:
10 .replace(/(\n *)\+(\[?[_a-zA-Z]+)/g, '$1readonly $2')
11 // Fix differently-named types:
12 .replace(/\bmixed\b/g, 'unknown')
13 .replace(/\| void\b/g, '| undefined')
14 // Fix `import type` syntax:
15 .replace(/\bimport type\b/g, 'import')
16 .replace(/\btype ([_a-zA-Z]+)( *[,\n}])/g, '$1$2')
17 // We aren't JS anymore:
18 .replace(/\/\/ @flow/, '')
19 .replace(/'(\.[^']*)\.js'/, "'$1'")
20
21 return output
22}
21module.exports = function jsToTs(js) {
22 var ts = babel.transform(js, {
23 plugins: (0, _loadPlugins.generatePlugins)()
24 }).code;
25
26 return ensureLastNewline(compactMultipleNewlines(ts));
27};

Related snippets