This commit is contained in:
2024-12-18 19:46:34 +00:00
parent dff15c66d5
commit eb7388e325
6 changed files with 795 additions and 0 deletions

View 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}`)