lesson2
This commit is contained in:
63
nodeJS/Learning/02/src/index.ts
Normal file
63
nodeJS/Learning/02/src/index.ts
Normal file
@@ -0,0 +1,63 @@
|
||||
let myName: string = "John Doe";
|
||||
let nothingMuch: undefined = undefined;
|
||||
|
||||
//Array
|
||||
//let arrayName: type[] =[value1, value2];
|
||||
let myArray: number[] = [1, 2, 3]
|
||||
let happyArray: Array<number> = [1, 2, 3]
|
||||
|
||||
//if change in the future, myArray = ...
|
||||
|
||||
//Tuples
|
||||
//Special type of array that allows you to specify the type of each element in that array
|
||||
//let tupleName: [type1, type2, type3] = [value1, value2, value3]
|
||||
|
||||
//Enums
|
||||
// Enums are a way to define a set of named constants
|
||||
// You only give me some options
|
||||
|
||||
enum Color {
|
||||
Red = "red",
|
||||
Green = 2,
|
||||
Blue = "blue",
|
||||
}
|
||||
|
||||
let myColor = Color.Blue
|
||||
//ts can show options
|
||||
|
||||
enum Direction {
|
||||
up = 1,
|
||||
down = 2,
|
||||
left = 3,
|
||||
right = 4,
|
||||
}
|
||||
|
||||
let playerDirection: Direction = Direction.right
|
||||
|
||||
// Type or Interface (can use interface instead of type), for objects
|
||||
// question mark, meaning optional and doesnt need to be there
|
||||
type character = {
|
||||
name: string;
|
||||
age: number;
|
||||
optional?: boolean;
|
||||
}
|
||||
|
||||
//unsure types
|
||||
//Any (normal javascript, can put anything)
|
||||
|
||||
//Union type
|
||||
|
||||
let myUnion: number | string = 123;
|
||||
myUnion = "Hello world";
|
||||
//myUnion = false; error cuz has to be number or string
|
||||
|
||||
//Functions
|
||||
|
||||
const addNumbers = (a: number, b: number, c:string): number => {
|
||||
console.log(c);
|
||||
let sum = a+b
|
||||
return sum;
|
||||
}
|
||||
|
||||
addNumbers(1,2, `We are adding these numbers ${1} and ${2}`)
|
||||
|
||||
Reference in New Issue
Block a user