From 5618725276e9528c2cda7d9ab1959d08cafc1dae Mon Sep 17 00:00:00 2001 From: Glen Whitney Date: Tue, 6 Sep 2022 21:36:20 -0700 Subject: [PATCH 1/2] feat: Attempt step one per roadmap --- .gitignore | 1 + package.json5 | 14 ++++++++++---- pnpm-lock.yaml | 15 +++++++++++++++ src/steps/one.ts | 22 ++++++++++++++++++++++ 4 files changed, 48 insertions(+), 4 deletions(-) create mode 100644 .gitignore create mode 100644 pnpm-lock.yaml create mode 100644 src/steps/one.ts diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b25c15b --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*~ diff --git a/package.json5 b/package.json5 index e55bd79..42f4b76 100644 --- a/package.json5 +++ b/package.json5 @@ -3,14 +3,20 @@ version: '0.0.0', description: 'Proof of concepts for a PocoMath-adjacent approach to a possible math.ts (TypeScript analogue of mathjs)', main: 'index.js', - 'scripts': { - test: 'echo "Error: no test specified" && exit 1' + scripts: { + test: 'echo "Error: no test specified" && exit 1', }, - keywords: ['math', 'typescript'], + keywords: [ + 'math', + 'typescript', + ], repository: { type: 'git', url: 'https://code.studioinfinity.org/glen/typomath.git', }, author: 'Glen Whitney', - license: 'Apache-2.0' + license: 'Apache-2.0', + dependencies: { + 'over.ts': 'github:m93a/over.ts', + }, } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml new file mode 100644 index 0000000..01f6172 --- /dev/null +++ b/pnpm-lock.yaml @@ -0,0 +1,15 @@ +lockfileVersion: 5.4 + +specifiers: + over.ts: github:m93a/over.ts + +dependencies: + over.ts: github.com/m93a/over.ts/0fd6e18afd4ca5a23c9e09d1fcd6b7357b642247 + +packages: + + github.com/m93a/over.ts/0fd6e18afd4ca5a23c9e09d1fcd6b7357b642247: + resolution: {tarball: https://codeload.github.com/m93a/over.ts/tar.gz/0fd6e18afd4ca5a23c9e09d1fcd6b7357b642247} + name: over.ts + version: 0.0.0 + dev: false diff --git a/src/steps/one.ts b/src/steps/one.ts new file mode 100644 index 0000000..7300e34 --- /dev/null +++ b/src/steps/one.ts @@ -0,0 +1,22 @@ +import { useTypes } from 'over'; + +const types = { + number: (x: unknown): x is number => typeof x === 'number', + bigint: (x: unknown): x is bigint => typeof x === 'bigint' +} + +const overload = useTypes(types) + +const add = overload({ + 'number,number -> number': (a,b) => { + console.log('Adding numbers', a, b) + return a+b + }, + 'bigint,bigint -> bigint': (a,b) => { + console.log('Adding bigints', a, b) + return a+b + } +}) + +console.log('Sum of 5 and 7 is', add(5,7)) +console.log('Sum of 5n and 7n is', add(5n, 7n)) -- 2.34.1 From 9c36ca3030b5f2d115163051244c8343a86ff8f2 Mon Sep 17 00:00:00 2001 From: Glen Whitney Date: Wed, 7 Sep 2022 02:06:13 -0400 Subject: [PATCH 2/2] feat: get step one working --- package.json5 | 1 + pnpm-lock.yaml | 8 ++++++++ src/steps/one.ts | 27 ++++++++++++++++++++++++--- tsconfig.json | 9 +++++++++ 4 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 tsconfig.json diff --git a/package.json5 b/package.json5 index 42f4b76..6c809b4 100644 --- a/package.json5 +++ b/package.json5 @@ -18,5 +18,6 @@ license: 'Apache-2.0', dependencies: { 'over.ts': 'github:m93a/over.ts', + typescript: '^4.8.2', }, } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 01f6172..f729120 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -2,12 +2,20 @@ lockfileVersion: 5.4 specifiers: over.ts: github:m93a/over.ts + typescript: ^4.8.2 dependencies: over.ts: github.com/m93a/over.ts/0fd6e18afd4ca5a23c9e09d1fcd6b7357b642247 + typescript: 4.8.2 packages: + /typescript/4.8.2: + resolution: {integrity: sha512-C0I1UsrrDHo2fYI5oaCGbSejwX4ch+9Y5jTQELvovfmFkK3HHSZJB8MSJcWLmCUBzQBchCrZ9rMRV6GuNrvGtw==} + engines: {node: '>=4.2.0'} + hasBin: true + dev: false + github.com/m93a/over.ts/0fd6e18afd4ca5a23c9e09d1fcd6b7357b642247: resolution: {tarball: https://codeload.github.com/m93a/over.ts/tar.gz/0fd6e18afd4ca5a23c9e09d1fcd6b7357b642247} name: over.ts diff --git a/src/steps/one.ts b/src/steps/one.ts index 7300e34..b9b3f86 100644 --- a/src/steps/one.ts +++ b/src/steps/one.ts @@ -1,4 +1,4 @@ -import { useTypes } from 'over'; +import { useTypes } from 'over.ts/src/index'; const types = { number: (x: unknown): x is number => typeof x === 'number', @@ -7,12 +7,26 @@ const types = { const overload = useTypes(types) +const negate = overload({ + 'number -> number': (a: number): number => { + console.log('Negating number', a) + return -a + }, + 'bigint -> bigint': (a: bigint): bigint => { + console.log('Negating bigint', a) + return -a + } +}) + +console.log('Negation of 5 is', negate(5)) +console.log('Negation of 5n is', negate(5n)) + const add = overload({ - 'number,number -> number': (a,b) => { + 'number, number -> number': (a: number, b: number): number => { console.log('Adding numbers', a, b) return a+b }, - 'bigint,bigint -> bigint': (a,b) => { + 'bigint, bigint -> bigint': (a: bigint, b: bigint): bigint => { console.log('Adding bigints', a, b) return a+b } @@ -20,3 +34,10 @@ const add = overload({ console.log('Sum of 5 and 7 is', add(5,7)) console.log('Sum of 5n and 7n is', add(5n, 7n)) + +try { + //@ts-expect-error + console.log('Mixed sum is', add(5n, 7)) +} catch { + console.log('Mixed sum errored as expected.') +} diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..d17d548 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,9 @@ +{ + "compilerOptions": { + "target": "ES2022", + "moduleResolution": "node" + }, + "files": [ + "src/steps/one.ts" + ] +} -- 2.34.1