Skip to content

Commit db0c9e7

Browse files
committed
Added typescript foundations
projects now page break on h2 for printing projects
1 parent a075dda commit db0c9e7

9 files changed

Lines changed: 284 additions & 2 deletions

File tree

language_reference/compose.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,7 @@ services:
3232
rust:
3333
build:
3434
context: ./languages/rust
35+
36+
typescript:
37+
build:
38+
context: ./languages/typescript

language_reference/languages/rust/rust.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
// https://doc.rust-lang.org/rust-by-example/ // VER: help
2+
13
use std::collections::HashMap; // VER: define_map
24
use std::collections::HashSet; // VER: define_set
35

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
*.js
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
FROM node:alpine
2+
3+
WORKDIR /typescript
4+
RUN npm install -g typescript @types/node && npx tsc --init
5+
6+
COPY *.ts ./
7+
8+
CMD tsc \
9+
--strict \
10+
--jsx preserve \
11+
--module esnext \
12+
--target esnext \
13+
--lib esnext,dom,dom.iterable \
14+
*.ts* \
15+
&& \
16+
node typescript.js
17+
18+
19+
#tsc --module esnext --target esnext --strict dom.tsx
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"use strict";
2+
const hasIterationProtocol = (variable) => variable !== null && Symbol.iterator in Object(variable);
3+
function h(type, params, children) {
4+
const el = document.createElement(type);
5+
for (let [k, v] of Object.entries(params)) {
6+
el.setAttribute(k, v);
7+
}
8+
if (["string", "number"].indexOf(typeof (children)) >= 0) {
9+
el.appendChild(document.createTextNode(children));
10+
}
11+
else if (hasIterationProtocol(children)) {
12+
for (let c of children) {
13+
el.append(c);
14+
}
15+
}
16+
else if (children) {
17+
el.appendChild(children);
18+
}
19+
return el;
20+
}
21+
const NAME = 'test';
22+
let temp = (<div>{NAME}</div>);
23+
console.log(`html: ${temp}`);
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
const hasIterationProtocol = (variable:any) => variable !== null && Symbol.iterator in Object(variable)
2+
function h(type: string, params: any, children: any[]) {
3+
const el = document.createElement(type)
4+
for (let [k,v] of Object.entries(params)) {el.setAttribute(k,(v as string))}
5+
if (["string","number"].indexOf(typeof(children))>=0) {el.appendChild(document.createTextNode(((children as unknown) as string)))}
6+
else if (hasIterationProtocol(children)) {for (let c of children) {el.append(c)}}
7+
else if (children ) {el.appendChild(children)}
8+
return el
9+
}
10+
11+
const NAME: string = 'test'
12+
let temp = (
13+
<div>{NAME}</div>
14+
);
15+
console.log(`html: ${temp}`);
Lines changed: 217 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,217 @@
1+
console.log('Hello World!');
2+
3+
4+
// https://www.w3schools.com/typescript/typescript_simple_types.php
5+
6+
let isActive: boolean = true;
7+
let hasPermission = false; // TypeScript infers 'boolean' type
8+
9+
let decimal: number = 6;
10+
let hex: number = 0xf00d; // Hexadecimal
11+
let binary: number = 0b1010; // Binary
12+
let octal: number = 0o744; // Octal
13+
let float: number = 3.14; // Floating point
14+
15+
let color: string = "blue";
16+
let fullName: string = 'John Doe';
17+
let age: number = 30;
18+
let sentence: string = `Hello, my name is ${fullName} and I'll be ${age + 1} next year.`;
19+
20+
const bigNumber: bigint = 9007199254740991n;
21+
const hugeNumber = BigInt(9007199254740991); // Alternative syntax
22+
23+
// Explicit type annotations
24+
// String
25+
let greeting: string = "Hello, TypeScript!";
26+
27+
// Number
28+
let userCount: number = 42;
29+
30+
// Boolean
31+
let isLoading: boolean = true;
32+
33+
// Array of numbers
34+
let scores: number[] = [100, 95, 98];
35+
36+
// Output the values
37+
console.log(greeting);
38+
console.log(userCount);
39+
console.log(isLoading);
40+
console.log(scores);
41+
42+
43+
// Function with explicit parameter and return types
44+
function greet(name: string): string {
45+
return `Hello, ${name}!`;
46+
}
47+
48+
// TypeScript will ensure you pass the correct argument type
49+
greet("Alice"); // OK
50+
//greet(42); // Error: Argument of type '42' is not assignable to parameter of type 'string'
51+
52+
// TypeScript infers the shape of the object
53+
const user = {
54+
name: "Alice",
55+
age: 30,
56+
isAdmin: true
57+
};
58+
59+
// TypeScript knows these properties exist
60+
console.log(user.name); // OK
61+
//console.log(user.email); // Error: Property 'email' does not exist
62+
63+
let u = true;
64+
//u = "string"; // Error: Type 'string' is not assignable to type 'boolean'.
65+
//Math.round(u); // Error: Argument of type 'boolean' is not assignable to parameter of type 'number'.
66+
67+
let v: any = true;
68+
v = "string"; // no error as it can be "any" type
69+
Math.round(v); // no error as it can be "any" type
70+
71+
72+
const names: string[] = [];
73+
names.push("Dylan"); // no error
74+
// names.push(3); // Error: Argument of type 'number' is not assignable to parameter of type 'string'.
75+
76+
const names2: readonly string[] = ["Dylan"];
77+
//names2.push("Jack"); // Error: Property 'push' does not exist on type 'readonly string[]'.
78+
// try removing the readonly modifier and see if it works?
79+
80+
const numbers = [1, 2, 3]; // inferred to type number[]
81+
numbers.push(4); // no error
82+
// comment line below out to see the successful assignment
83+
//numbers.push("2"); // Error: Argument of type 'string' is not assignable to parameter of type 'number'.
84+
let head: number = numbers[0]; // no error
85+
86+
87+
// define our tuple
88+
let ourTuple: [number, boolean, string];
89+
90+
// initialize correctly
91+
ourTuple = [5, false, 'Coding God was here'];
92+
93+
// define our tuple
94+
let ourTuple2: [number, boolean, string];
95+
// initialized incorrectly which throws an error
96+
//ourTuple2 = [false, 'Coding God was mistaken', 5];
97+
98+
const car: { type: string, model: string, year: number } = {
99+
type: "Toyota",
100+
model: "Corolla",
101+
year: 2009
102+
};
103+
104+
enum CardinalDirections {
105+
North,
106+
East,
107+
South,
108+
West
109+
}
110+
let currentDirection = CardinalDirections.North;
111+
// logs 0
112+
console.log(currentDirection);
113+
// throws error as 'North' is not a valid enum
114+
//currentDirection = 'North'; // Error: "North" is not assignable to type 'CardinalDirections'.
115+
116+
type CarYear = number
117+
type CarType = string
118+
type CarModel = string
119+
type Car = {
120+
year: CarYear,
121+
type: CarType,
122+
model: CarModel
123+
}
124+
125+
const carYear: CarYear = 2001
126+
const carType: CarType = "Toyota"
127+
const carModel: CarModel = "Corolla"
128+
const car2: Car = {
129+
year: carYear,
130+
type: carType,
131+
model: carModel
132+
};
133+
134+
type Animal = { name: string };
135+
type Bear = Animal & { honey: boolean };
136+
const bear: Bear = { name: "Winnie", honey: true };
137+
138+
type Status = "success" | "error";
139+
let response: Status = "success";
140+
141+
interface Rectangle {
142+
height: number,
143+
width: number
144+
}
145+
146+
const rectangle: Rectangle = {
147+
height: 20,
148+
width: 10
149+
};
150+
151+
function printStatusCode(code: string | number) {
152+
console.log(`My status code is ${code}.`)
153+
}
154+
printStatusCode(404);
155+
printStatusCode('404');
156+
157+
// the `: number` here specifies that this function returns a number
158+
function getTime(): number {
159+
return new Date().getTime();
160+
}
161+
162+
function printHello(): void {
163+
console.log('Hello!');
164+
}
165+
166+
function multiply(a: number, b: number) {
167+
return a * b;
168+
}
169+
170+
// the `?` operator here marks parameter `c` as optional
171+
function add(a: number, b: number, c?: number) {
172+
return a + b + (c || 0);
173+
}
174+
175+
function pow(value: number, exponent: number = 10) {
176+
return value ** exponent;
177+
}
178+
179+
function divide({ dividend, divisor }: { dividend: number, divisor: number }) {
180+
return dividend / divisor;
181+
}
182+
183+
function add2(a: number, b: number, ...rest: number[]) {
184+
return a + b + rest.reduce((p, c) => p + c, 0);
185+
}
186+
187+
type Negate = (value: number) => number;
188+
// in this function, the parameter `value` automatically gets assigned the type `number` from the type `Negate`
189+
const negateFunction: Negate = (value) => value * -1;
190+
191+
192+
let x: unknown = 'hello';
193+
console.log((x as string).length);
194+
195+
class Person {
196+
private name: string;
197+
198+
public constructor(name: string) {
199+
this.name = name;
200+
}
201+
202+
public getName(): string {
203+
return this.name;
204+
}
205+
}
206+
207+
const person = new Person("Jane");
208+
console.log(person.getName()); // person.name isn't accessible from outside the class since it's private
209+
210+
function createPair<S, T>(v1: S, v2: T): [S, T] {
211+
return [v1, v2];
212+
}
213+
console.log(createPair<string, number>('hello', 42)); // ['hello', 42]
214+
215+
// cast
216+
let xx: unknown = 'hello';
217+
console.log((<string>xx).length); // <> syntax does not seen to work in jsx (good choice of symbol?)

make_ver/make_ver.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ class Language(NamedTuple):
169169
('lua',('lua',),(Comment(r'--'),)),
170170
('golang',('go',),COMMENTS_STYLE_C),
171171
('rust',('rs',),COMMENTS_STYLE_C),
172+
('typescript',('ts',),COMMENTS_STYLE_C),
172173
# txt = '#',
173174
))
174175
for language_ext in language.ext

static/projects.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515

1616
@media print {
1717
@page {margin: 1em;}
18-
h1 {page-break-before: always;}
19-
h1:first-of-type {page-break-before: avoid;}
18+
h2 {page-break-before: always;}
19+
h2:first-of-type {page-break-before: avoid;}
2020
table {page-break-inside:auto;}
2121
tr {page-break-inside:avoid; page-break-after:auto;}
2222
button {display: none;} /* and (min-width: 500px) */

0 commit comments

Comments
 (0)