typescript
/

TypeScript Syntax

Last Sync: Today

On this page

11
0%
5 min read
Remaining
5 minleft

Click any section to jump — progress syncs automatically

typescript

TypeScript Syntax

What is TypeScript Syntax?

TypeScript syntax extends JavaScript by adding types and modern language features for better code structure.

Variables with Types

TypeScriptRead-only
1
let name: string = 'John';
let age: number = 25;
let isActive: boolean = true;

Type Inference

TypeScriptRead-only
1
let message = 'Hello'; // inferred as string

Functions

TypeScriptRead-only
1
function add(a: number, b: number): number {
  return a + b;
}

Arrow Functions

TypeScriptRead-only
1
const greet = (name: string): string => {
  return `Hello ${name}`;
};

Arrays

TypeScriptRead-only
1
let numbers: number[] = [1, 2, 3];
let names: Array<string> = ['A', 'B'];

Objects

TypeScriptRead-only
1
let user: { name: string; age: number } = {
  name: 'John',
  age: 30
};

Any Type

TypeScriptRead-only
1
let data: any = 'Hello';
data = 123;

Best Practices

  • Use explicit types where needed
  • Avoid overusing any type
  • Use type inference wisely
  • Write clean and readable code

Common Mistakes

  • Overusing any type
  • Ignoring type errors
  • Incorrect type definitions
  • Mixing types inconsistently

Conclusion

Understanding TypeScript syntax is essential for writing type-safe and scalable applications.

Try it yourself

let num: number = 10;
console.log(num);

Test Your Knowledge

Q1
of 3

TypeScript adds?

A
Styles
B
Types
C
HTML
D
Database
Q2
of 3

Function return type?

A
After :
B
Before function
C
Inside {}
D
Not needed
Q3
of 3

Array type?

A
number[]
B
array
C
list
D
numbers

Frequently Asked Questions

What is type inference?

TypeScript automatically detects type.

What is any type?

A flexible type that allows any value.

Are types mandatory?

No, but recommended.

Previous

ts setup

Next

ts types

Related Content

Need help?

Explore our comprehensive docs or start a chat with our tech experts.