typescript
/

TypeScript Types

Last Sync: Today

On this page

11
0%
5 min read
Remaining
5 minleft

Click any section to jump — progress syncs automatically

typescript

TypeScript Types

What are Types?

Types in TypeScript define the kind of data a variable can hold, ensuring type safety and better code quality.

Primitive Types

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

Array Types

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

Tuple

TypeScriptRead-only
1
let user: [string, number] = ['John', 30];

Enum

TypeScriptRead-only
1
enum Role {
  Admin,
  User,
  Guest
}

Any Type

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

Union Types

TypeScriptRead-only
1
let id: string | number;
id = 101;
id = 'ABC';

Type Aliases

TypeScriptRead-only
1
type User = {
  name: string;
  age: number;
};

Best Practices

  • Use specific types instead of any
  • Use union types for flexibility
  • Define reusable type aliases
  • Keep types simple and readable

Common Mistakes

  • Overusing any type
  • Incorrect union usage
  • Not defining proper types
  • Ignoring type errors

Conclusion

TypeScript types help create safer and more maintainable code by enforcing data structure and reducing runtime errors.

Try it yourself

let id: string | number = 101;

Test Your Knowledge

Q1
of 3

Union type symbol?

A
&
B
|
C
:
D
=
Q2
of 3

Tuple means?

A
Array
B
Fixed types
C
Object
D
Enum
Q3
of 3

Avoid which type?

A
string
B
number
C
any
D
boolean

Frequently Asked Questions

What are types?

They define the data a variable can hold.

What is union type?

A variable can hold multiple types.

What is tuple?

An array with fixed types.

Previous

ts syntax

Next

ts variables

Related Content

Need help?

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