From 701893418c00928af20f7dde4f3946c23b803741 Mon Sep 17 00:00:00 2001 From: Matt Pocock Date: Mon, 11 Sep 2023 16:05:17 +0100 Subject: [PATCH] 2023-09-11T15:05:17.905Z --- ...nums-cross-value-and-type-world.problem.ts | 0 ...ums-cross-value-and-type-world.solution.ts | 0 ...sses-cross-value-and-type-world.problem.ts | 30 ++++++++++++++++++ ...ses-cross-value-and-type-world.solution.ts | 31 +++++++++++++++++++ ...-crosses-value-and-type-world.explainer.ts | 19 ++++++++++++ ...is-crosses-value-and-type-world.problem.ts | 0 ...s-crosses-value-and-type-world.solution.ts | 0 7 files changed, 80 insertions(+) delete mode 100644 src/040-deriving-types-from-values/128-classes-and-enums-cross-value-and-type-world.problem.ts delete mode 100644 src/040-deriving-types-from-values/128-classes-and-enums-cross-value-and-type-world.solution.ts create mode 100644 src/040-deriving-types-from-values/128-classes-cross-value-and-type-world.problem.ts create mode 100644 src/040-deriving-types-from-values/128-classes-cross-value-and-type-world.solution.ts create mode 100644 src/040-deriving-types-from-values/129-this-crosses-value-and-type-world.explainer.ts delete mode 100644 src/040-deriving-types-from-values/129-this-crosses-value-and-type-world.problem.ts delete mode 100644 src/040-deriving-types-from-values/129-this-crosses-value-and-type-world.solution.ts diff --git a/src/040-deriving-types-from-values/128-classes-and-enums-cross-value-and-type-world.problem.ts b/src/040-deriving-types-from-values/128-classes-and-enums-cross-value-and-type-world.problem.ts deleted file mode 100644 index e69de29..0000000 diff --git a/src/040-deriving-types-from-values/128-classes-and-enums-cross-value-and-type-world.solution.ts b/src/040-deriving-types-from-values/128-classes-and-enums-cross-value-and-type-world.solution.ts deleted file mode 100644 index e69de29..0000000 diff --git a/src/040-deriving-types-from-values/128-classes-cross-value-and-type-world.problem.ts b/src/040-deriving-types-from-values/128-classes-cross-value-and-type-world.problem.ts new file mode 100644 index 0000000..2a6d592 --- /dev/null +++ b/src/040-deriving-types-from-values/128-classes-cross-value-and-type-world.problem.ts @@ -0,0 +1,30 @@ +import { describe, expect, it } from "vitest"; + +class CanvasNode { + x = 0; + y = 0; + + move(x: number, y: number) { + this.x = x; + this.y = y; + } +} + +const positionFromCanvasNode = (node) => { + return { + x: node.x, + y: node.y, + }; +}; + +describe("positionFromCanvasNode", () => { + it("Should return the position of the node", () => { + const canvasNode = new CanvasNode(); + + expect(positionFromCanvasNode(canvasNode)).toEqual({ x: 0, y: 0 }); + + canvasNode.move(10, 20); + + expect(positionFromCanvasNode(canvasNode)).toEqual({ x: 10, y: 20 }); + }); +}); diff --git a/src/040-deriving-types-from-values/128-classes-cross-value-and-type-world.solution.ts b/src/040-deriving-types-from-values/128-classes-cross-value-and-type-world.solution.ts new file mode 100644 index 0000000..cb9a889 --- /dev/null +++ b/src/040-deriving-types-from-values/128-classes-cross-value-and-type-world.solution.ts @@ -0,0 +1,31 @@ +import { describe, expect, it } from "vitest"; + +class CanvasNode { + x = 0; + y = 0; + + move(x: number, y: number) { + this.x = x; + this.y = y; + } +} + +// Look at typeof CanvasNode +const positionFromCanvasNode = (node: CanvasNode) => { + return { + x: node.x, + y: node.y, + }; +}; + +describe("positionFromCanvasNode", () => { + it("Should return the position of the node", () => { + const canvasNode = new CanvasNode(); + + expect(positionFromCanvasNode(canvasNode)).toEqual({ x: 0, y: 0 }); + + canvasNode.move(10, 20); + + expect(positionFromCanvasNode(canvasNode)).toEqual({ x: 10, y: 20 }); + }); +}); diff --git a/src/040-deriving-types-from-values/129-this-crosses-value-and-type-world.explainer.ts b/src/040-deriving-types-from-values/129-this-crosses-value-and-type-world.explainer.ts new file mode 100644 index 0000000..4430dba --- /dev/null +++ b/src/040-deriving-types-from-values/129-this-crosses-value-and-type-world.explainer.ts @@ -0,0 +1,19 @@ +class CanvasNode { + x: number; + y: number; + + constructor(x: number, y: number) { + this.x = x; + this.y = y; + } + + move(x: number, y: number): this { + this.x += x; + this.y += y; + return this; + } +} + +const node = new CanvasNode(0, 0).move(10, 20).move(30, 40); + +type test = Expect>; diff --git a/src/040-deriving-types-from-values/129-this-crosses-value-and-type-world.problem.ts b/src/040-deriving-types-from-values/129-this-crosses-value-and-type-world.problem.ts deleted file mode 100644 index e69de29..0000000 diff --git a/src/040-deriving-types-from-values/129-this-crosses-value-and-type-world.solution.ts b/src/040-deriving-types-from-values/129-this-crosses-value-and-type-world.solution.ts deleted file mode 100644 index e69de29..0000000