Skip to content

Commit 82a1009

Browse files
committed
working in sandbox
1 parent 6f2ed70 commit 82a1009

File tree

2 files changed

+84
-12
lines changed

2 files changed

+84
-12
lines changed

sandbox.js

Lines changed: 39 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,39 @@
1-
var character = 'mario';
2-
console.log(character);
3-
var inputs = document.querySelectorAll('input');
4-
inputs.forEach(function (input) {
5-
console.log(input);
6-
});
1+
var character = "mario";
2+
//character inferred to always be a string
3+
var age = 30;
4+
//age always inferred to be a number
5+
var isBlackBelt = false;
6+
//isBlackBelt always inferred to be a boolean
7+
//character = 20 not allowed because it isn't type: string
8+
character = "luigi"; //is allowed because the reassignment is to the same type
9+
// type a parameter being passed into a function with trailing : followed by the type
10+
var circumference = function (diameter) {
11+
return diameter * Math.PI;
12+
};
13+
//when invoked with an argument this will error out unless the correct type is passed in
14+
console.log(circumference(7.5));
15+
//arrays
16+
var names = ["luigi", "mario", "yoshi"];
17+
names.push("toad"); //works because it matches the types in the array when it was instantiated
18+
// names.push(3) --> errors out because 3 isn't type: string
19+
// names[0] = 3; --> also disallowed because we're trying to reassign an index that was type: string to a number
20+
//can do a mixed array
21+
var mixedArray = ["ken", 4, "chun-li", 8, 9, false, { name: "shane" }];
22+
mixedArray.push("kirk");
23+
mixedArray.push(3);
24+
mixedArray[0] = 99;
25+
//objects
26+
var ninja = {
27+
name: "mario",
28+
belt: "black",
29+
age: 30
30+
};
31+
ninja.age = 40; //this is fine, updating property with type: string with another string
32+
// ninja.name = 40 --> won't work, reassigning string to number once it was already defined
33+
//this is allowed because ninja has the same properties and same types on those porperties
34+
// can't change name to username, would error out and can't change the types of any of the property values. You have to use the initial structure of the object from when it was declared
35+
ninja = {
36+
name: "yoshi",
37+
belt: "yellow",
38+
age: 40
39+
};

sandbox.ts

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,48 @@
1-
const character = 'mario';
1+
let character = "mario";
2+
//character inferred to always be a string
3+
let age = 30;
4+
//age always inferred to be a number
5+
let isBlackBelt = false;
6+
//isBlackBelt always inferred to be a boolean
27

3-
console.log(character);
8+
//character = 20 not allowed because it isn't type: string
9+
character = "luigi"; //is allowed because the reassignment is to the same type
410

5-
const inputs = document.querySelectorAll('input');
11+
// type a parameter being passed into a function with trailing : followed by the type
12+
const circumference = (diameter: number) => {
13+
return diameter * Math.PI;
14+
};
615

7-
inputs.forEach(input => {
8-
console.log(input);
9-
});
16+
//when invoked with an argument this will error out unless the correct type is passed in
17+
console.log(circumference(7.5));
18+
19+
//arrays
20+
let names = ["luigi", "mario", "yoshi"];
21+
22+
names.push("toad"); //works because it matches the types in the array when it was instantiated
23+
// names.push(3) --> errors out because 3 isn't type: string
24+
// names[0] = 3; --> also disallowed because we're trying to reassign an index that was type: string to a number
25+
//can do a mixed array
26+
let mixedArray = ["ken", 4, "chun-li", 8, 9, false, { name: "shane" }];
27+
mixedArray.push("kirk");
28+
mixedArray.push(3);
29+
mixedArray[0] = 99;
30+
31+
//objects
32+
let ninja = {
33+
name: "mario",
34+
belt: "black",
35+
age: 30,
36+
};
37+
38+
ninja.age = 40; //this is fine, updating property with type: string with another string
39+
40+
// ninja.name = 40 --> won't work, reassigning string to number once it was already defined
41+
42+
//this is allowed because ninja has the same properties and same types on those porperties
43+
// can't change name to username, would error out and can't change the types of any of the property values. You have to use the initial structure of the object from when it was declared
44+
ninja = {
45+
name: "yoshi",
46+
belt: "yellow",
47+
age: 40,
48+
};

0 commit comments

Comments
 (0)