Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions 10 generics/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
/** A FIFO (First In First Out) collection */
class Queue<T> {
data = [];
public data:T[] = [];
push(item: T) { this.data.push(item); }
pop(): T { return this.data.shift(); }
pop(): T { return this.data.shift()! ; } // ! signifies that it can't be undefined
}

const queue = new Queue<number>();
queue.push(123);
queue.push(456);

console.log(queue.pop().toPrecision(1));
console.log(queue.pop().toPrecision(1));
console.log(queue.pop().toPrecision(1));
4 changes: 2 additions & 2 deletions 27 narrowing/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ type Rectangle = {

type Shape = Square | Rectangle;

function area(shape: Shape) {
function area(shape: Shape): void|number {
if ('size' in shape) {
return shape.size * shape.size;
}
Expand All @@ -19,4 +19,4 @@ function area(shape: Shape) {
}

area({ size: 2 }); // 4
area({ width: 2, height: 3 }); // 6
area({ width: 2, height: 3 }); // 6