[go: up one dir, main page]

0% found this document useful (0 votes)
53 views4 pages

Typescript Notes: Types Primitive String Bool Number Special Arrays Any Function Params and Props Objects

The document discusses different typescript types including primitive types like string, boolean, and number. It also covers special types like arrays, objects, functions, and unions. It provides examples of declaring types for variables and functions. Additionally, it outlines type operators like readonly, intersections, optional properties, enums, type aliases, interfaces, and type assertions.

Uploaded by

frank salad
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views4 pages

Typescript Notes: Types Primitive String Bool Number Special Arrays Any Function Params and Props Objects

The document discusses different typescript types including primitive types like string, boolean, and number. It also covers special types like arrays, objects, functions, and unions. It provides examples of declaring types for variables and functions. Additionally, it outlines type operators like readonly, intersections, optional properties, enums, type aliases, interfaces, and type assertions.

Uploaded by

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

Thursday, April 15, 2021

TYPESCRIPt NOTES
TYPES

Primitive

- String
- Bool
- Number
SPECIAL

- Arrays
- Any
- Function params and props
- Objects
Declaring Type Example Todo

type Todo = {

id: number,

text: string,

done: boolean,

SKELETON

- FUNCTION

• Function(param:<type>) : <Return Type> {

return

1
Thursday, April 15, 2021

Mapped Keywords

ReadOnly=> prevents modifying {makes all props in


object readonly}. ReadOnly<…>

type Todo = ReadOnly<{

id: number,

text: string,

done: boolean,

}>

Literal Types

done: true

Arrays in function syntax


fx(x:X[]) : X[]

TYPEOF GUARDS

• "string"

• "number"

• "bigint"

• "boolean"

• "symbol"

• "unde ned"

• "object"

• "function"

2
fi
Thursday, April 15, 2021

INTERSECTING TYPES & TYPE OPERATORS

type = A & B.

JOINS both types into new type

UNIONS

type foo = number | string

const a: Foo = 1

const a: Foo = “home”

FOO TYPE CAN BE EITHER A NUMBER OR STRING

Use typeof in an if else statement to narrow union

OPTIONAL

? After property for optional

INTERFACE - explicitly describe the object or class shape

interface User {
name: string;
id: number;
}

Type Aliases

SAME AS INTERFACE BUT CAN’T BE RE-OPENED/CHANGED

ENUMS

NUMBERS—auto increment

enum Direction {

Up = 1,

Down //2

Left//3

Right//4

STRINGS—> good for debugging

enum Directions {

Up = “UP”,

Thursday, April 15, 2021


Down = “DOWN”,

Left = “LEFT”,

Right = “RIGHT”

TYPE ASSERTIONS

Var foo = <foo>bar;

IN REACT/JSX

Var foo = bar as foo

You might also like