0% found this document useful (0 votes)
3K views5 pages

TYPESCRIPT

typescript basics

Uploaded by

fa21-bcs-139
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3K views5 pages

TYPESCRIPT

typescript basics

Uploaded by

fa21-bcs-139
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

TYPESCRIPT

DATATYPES

1. BASIC TYPES

NUMBER: REPRESENTS NUMERICAL VALUES.


let age: number = 25;

STRING: REPRESENTS TEXTUAL DATA.


let name: string = "Alice";

BOOLEAN: REPRESENTS TRUE OR FALSE VALUES.


let isStudent: boolean = true;

ANY: CAN HOLD ANY TYPE OF VALUE.


let anything: any = 42;

anything = "Hello";

VOID: USED FOR FUNCTIONS THAT DO NOT RETURN A VALUE.


function logMessage(message: string): void {

console.log(message);

NULL AND UNDEFINED: REPRESENT ABSENCE OF VALUE.


let nothing: null = null;

let notDefined: undefined = undefined;

2. ARRAY

Arrays can hold multiple values of the same type.


NUMBER[]: ARRAY OF NUMBERS.
let numbers: number[] = [1, 2, 3];

STRING[]: ARRAY OF STRINGS.


let names: string[] = ["Alice", "Bob", "Charlie"];

3. TUPLE
Tuples allow you to express an array with a fixed number of elements whose types are known.

let person: [string, number] = ["Alice", 25];

4. ENUM
Enums allow a developer to define a set of named constants.

enum Color {

Red,

Green,

Blue

let c: Color = Color.Green;

5. OBJECT
Objects are collections of key-value pairs.

let user: { name: string; age: number } = {

name: "Alice",

age: 25

};

6. UNION TYPES
Union types allow a variable to have one of several types.

let value: number | string;


value = 42; // OK

value = "Hello"; // OK

7. TYPE ALIASES
Type aliases create a new name for a type.

type ID = number | string;

let userId: ID;

userId = 123; // OK

userId = "abc"; // OK

8. INTERFACES
Interfaces define the structure of an object. They are similar to type aliases but are more powerful when
it comes to describing object shapes.

interface User {

name: string;

age: number;

let user: User = {

name: "Alice",

age: 25

};

9. FUNCTIONS
TypeScript allows you to specify types for function parameters and return values.

function add(a: number, b: number): number {

return a + b;

10. GENERICS
Generics allow you to create reusable components.
function identity<T>(arg: T): T {

return arg;

let output = identity<string>("Hello");

let outputNumber = identity<number>(42);

KEY CONCEPTS TO KNOW:

1. Type Inference: TypeScript automatically infers the type based on the value assigned.

let x = 10; // inferred as number

2. Type Assertion: Tells the compiler to treat a value as a specified type.

let someValue: any = "Hello World";

let strLength: number = (someValue as string).length;

3. Optional Properties: Properties that may or may not be present in an object.

interface Person {

name: string;

age?: number; // age is optional

4. Readonly Properties: Properties that cannot be modified after being set.

interface Car {

readonly make: string;

model: string;

let myCar: Car = { make: "Toyota", model: "Corolla" };

// myCar.make = "Honda"; // Error: Cannot assign to 'make' because it is a read-only property.

You might also like