Online Javascript to Typescript Converter
This is a utility tool to convert a JavaScript codebase to TypeScript, while trying to solve some of the
common TypeScript errors that will be received upon such a conversion.
Basic variable types
| Type |
Example |
Notes |
| number |
let myNumber: number = 6 |
Represent all numbers : integer, float, hex etc. |
| string |
let fullName: string = Bob Bobbington |
Text with all text functions (indexOf, replace etc.) |
| boolean |
let isTrue: boolean = true |
true/false value |
| Array |
let list: Array = [1, 2, 3] |
Shorter version: let list: number[] = [1, 2, 3]; |
| Tuple |
let x: [string, number] = ['test', 2]; |
Array contains ains items with various types |
| any |
let x: any = 2; |
Everything can be assigned to value with any type |
| Enum |
enum Color {Red, Blue } |
Usage: let c: Color = Color.Blue; |
| Never |
function fail(): never {while(true) {}} |
Indicating that something never should happen |
| Null or undefined |
let u: undefined = undefined; |
In TS there is dedicated type for null and undefined |
| Object |
let x:Object = {id: 2}; |
Represents any object |
| Function |
let myFn:Function = function() {...} |
Represents any function |
Functions
| Concept |
Code |
| Function |
{ (arg1 :Type, argN :Type) :Type; } or (arg1 :Type, argN :Type) => Type; |
| Constructor |
{ new () :ConstructedType; } or new () => ConstructedType; |
| Function type with optional param |
(arg1 :Type, optional? :Type) => ReturnType; |
Function type with rest param |
(arg1 :Type, ...allOtherArgs :Type[]) => ReturnType; |
| Function type with static property |
{ () :Type; staticProp :Type; } |
| Default argument |
function fn(arg1 :Type = 'default') :ReturnType {} |
| Arrow function |
(arg1 :Type) :ReturnType =>; {} or (arg1 :Type) :ReturnType =>; Expression |
rimitive Types
| Type |
Code |
| Any type (explicitly untyped) |
any |
| void type (null or undefined, use for function returns only) |
void |
| String |
string |
| Number |
number |
| Boolean |
boolean |