In dynamically typed languages, the types are associated with run-time values and not named explicitly in your code In statically typed languages, you explicitly assign types to variables, function parameters, return values, etc
Static Examples: Java, C, C++, Rust, Go Dynamic Examples: JavaScript, Python, Ruby, PHP
PROS | CONS |
---|---|
More Robust | More Code To Write |
Easily Spot Bugs | More To Learn |
Predictability | Required Compilation |
Readability | Not True Static Typing |
Popular |
-
- TypeScript uses .ts and .tsx extensions
- TSC (TypeScript Compiler) is used to compile .ts files down to JS
- Can watch files and report errors at compile time
- Many tools include TS compilation by default
- Most IDEs have great support for TS
- The tsconfig.json file is used to configure how TypeScript works
-
tsc --init
- In tsconfig.json
"target": "ES6"
to change version of JS. "rootDir"
is where ts files will reside"outDir"
is where js files will reside
-
-
number
-
string
-
boolean
-
any
-> dynamic type -
array
-><type>[]
- Example:
let ids: number[] = [1, 2, 3, 4, 5]
any
as a type for a array is valid, it will support dynamic type.tuple
is when we want specific types elements to go in a specific order in an array.- Example:
let person: [number, string, boolean] = [1, 'Brad', false ]
- Example:
-
union '|'
-> to define more than 1 type to a variable / function parameter. -
-
Enums allow a developer to define a set of named constants. Using enums can make it easier to document intent, or create a set of distinct cases. TypeScript provides both numeric and string-based enums.
-
just access any member as a property off of the enum itself, and declare types using the name of the enum
-
-
Objects
-
Objects are like normal objects but we can define types for their properties.
-
type
orinterface
can also be used to define types for the properties.
-
-
Interface
- Difference between a
type
andinterface
is that, we cannot use interface withunion
orprimitives
, i.e.
+ type Point = number | string; - interface Point = number | string ;
-
?
in front of a type key (property) in an interface makes it optional. -
We can make a property read-only by adding
readonly
prefix to it. -
Syntax to add interface to a class:
class <classname> implements <interface_name>
- Difference between a
-
Type Assertion
- over-riding type of an entity while inheriting it.
- way #1:
<type>element
- way #2:
element as type
-
Functions
- Syntax:
function name(param: type): return-type {...}
- If there is no return type, use
void
as the return type.
- Syntax:
-
Class
constructor
is used to assign values to properties of a class.- when extending a class (sub-class)
super
is used to pass values to the properties which are inherited from parent class.
-
Generics
- Makes array dynamic on the go.
<Type> / <T>
is used to denote generic type.
-