Private archive copy · Unofficial site · Content copyright belongs to Runoob Tutorial and the original authors · View Original

TypeScript vs JavaScript Comparison

TypeScript vs JavaScript Comparison

TypeScript and JavaScript are the two most important languages in front-end development; understanding the differences between them is essential for modern web development.

JavaScript is the native scripting language of the Web, while TypeScript is a superset of JavaScript that adds a type system and other advanced features.


Core difference

Feature JavaScript TypeScript
Type system dynamic typing Static typing (optional)
Compilation Interpreted execution Compiled to JavaScript
Type checking at development time None Yes
IDE support Basics Powerful IntelliSense
Code Refactoring Difficult Safe and simple
TypeScript is a superset of JavaScript JavaScript dynamic typing Interpreted execution No compilation needed TypeScript Static typing (optional) Compiled to JS Type checking Smart hints Inclusion relationship All JS code is valid TS

Type system comparison

JavaScript is a dynamically typed language; variable types are determined at runtime:

JavaScript dynamic typing

// JavaScript - variable types can change at any time

var

message

=

"Hello"

;

message

=

123

;

         

// valid, no error

message

=

true

;

         

// still valid

// variable types are unknown until runtime

function

greet

(

name

)

{

   

return

"Hello, "

name

;

}

TypeScript is a statically typed language; types are checked at compile time:

TypeScript static typing

// TypeScript - specify the type at declaration

var

message

:

string

=

"Hello"

;

// message = 123; // Compile error: Type 'number' is not assignable to type 'string'

// function parameter and return types are explicit

function

greet

(

name

:

string

)

:

string

{

   

return

"Hello, "

name

;

}

// type errors are found at compile time

greet

(

123

)

;

 

// Compilation error: Argument of type 'number' is not assignable to parameter of type 'string'


Compilation process

TypeScript must be compiled to JavaScript to run in the browser:

TypeScript source code

// app.ts - TypeScript source code

interface

User

{

    name

:

string

;

    age

:

number

;

}

function

createUser

(

name

:

string

,

age

:

number

)

:

User

{

   

return

{

name

,

age

}

;

}

var

user

=

createUser

(

"Alice"

,

25

)

;

console.

log

(

user

)

;

Compiled JavaScript:

Compiled JavaScript

// app.js - compiled JavaScript

function

createUser

(

name

,

age

)

{

   

return

{

name

:

name

,

age

:

age

}

;

}

var

user

=

createUser

(

"Alice"

,

25

)

;

console.

log

(

user

)

;


Type Inference

TypeScript has powerful type inference capabilities, even without explicit type declarations:

Type Inference

// TypeScript automatically infers types

var

num

=

10

;

         

// Inferred as number

var

str

=

"hello"

;

   

// Inferred as string

var

isActive

=

true

;

 

// Inferred as boolean

// function return types are also inferred

function

add

(

a

,

b

)

{

   

return

a

b

;

   

// return value inferred as number

}

var

result

=

add

(

1

,

2

)

;

 

// result is of type number


Development experience comparison

Smart hints

TypeScript provides rich type information to IDEs, enabling IntelliSense:



// In TypeScript, the IDE knows the user object has name and age properties

user.    // 自动提示 .name 和 .age



// JavaScript in IDE cannot determine type, limited suggestions

user.    // 可能没有有用的提示

Refactoring support

TypeScript makes code refactoring safer:


Reasons to choose TypeScript

Why choose TypeScript:

  1. Compile-time error detection, finding bugs early
  2. Better code readability and maintainability
  3. Powerful IDE IntelliSense
  4. Safe refactoring
  5. Standard for modern front-end frameworks (React, Vue, Angular)
  6. TypeScript is a superset of JavaScript; while maintaining JavaScript's flexibility, it adds a type system to improve code quality. For large projects, TypeScript's advantages are even more evident.