From 79e8387cf2afd789b8ea0bee2305fe5b3b3d339d Mon Sep 17 00:00:00 2001 From: anderium <33520919+anderium@users.noreply.github.com> Date: Tue, 23 Jan 2024 01:50:38 +0100 Subject: [PATCH] Remove some @ts-expect-errors for mapping types This does some hacky things to get inference that works for the mapping types. Based on: https://github.com/microsoft/TypeScript/pull/47109 Also adds two comments where the expect error cannot reasonably be removed (as far as I can see). Experiment with it here: https://www.typescriptlang.org/play?ts=5.3.3#code/C4TwDgpgBAYglgJwgEwPKQQQ2HA9gOygF4oBvKUSALigCIAzRFWgGigGMIAbLm-AVwC2AIwgIoAXwBQlaAFlcANxTox2PIRLlZNWoKXM2nHnyGiEbMLgDOcHARoBtASLFsX5gLqSZ4aKqx7TVgmNAx1AigAHygFZTC1IKlkgHoUij8oAIjg8kYkZBpUQTsAHngC7KC2WllaAD42fXiikuBSuJVw6ro6+skAbl9ILO6NABVMkiqNR1q-Wk9k2VHEjTlMMFKAaSgIAA9gCHxka1XAib9+rShHAAUoOEJtzxpSKShPjOooO5YpCRQABkWTapQAoocsOx2jMCGxSDpfhJGr0FvUAY4XkNhtAYPx8DD1psdntDsdTuccpNINcyB8vvdHs9XlAABS4MYOKlBDZbO71ACUxH6ilwcGQAOS7AI1mAUHoBKJBD5NHxhN5JLh+BpEDp7y+CtCNDZ+RQAH1OWtuRUutb8IKaGKJSL6YbDWbkJaufgAHTGLgMz4Sf6G5ooE3hr1Wi7czoJWMOp3i5Cug3uz5R732-3cQMZzMGaM+31WWxJQ3SaRSRUajRQTZgLggbWkg5HE5nbW6+ocn1FH18nZC5Mu9OfGX4OVQfbEKAxnK+2RBjiy+W19hzjeasCOfZLD1Kvv2wVSoA Signed-off-by: anderium <33520919+anderium@users.noreply.github.com> --- source/arroost/components/tunnel.js | 5 +-- source/arroost/entities/arrows/connection.js | 1 + source/arroost/entities/arrows/reality.js | 2 +- source/nogan/declare.d.ts | 34 +++++++++++++------- source/nogan/nogan.js | 12 +++---- source/nogan/operate.js | 2 +- 6 files changed, 34 insertions(+), 22 deletions(-) diff --git a/source/arroost/components/tunnel.js b/source/arroost/components/tunnel.js index 9808823..9311478 100644 --- a/source/arroost/components/tunnel.js +++ b/source/arroost/components/tunnel.js @@ -66,11 +66,12 @@ export class Tunnel extends Component { } /** - * @param {Operation} operation + * Typing based on: https://github.com/microsoft/TypeScript/pull/47109 + * @template {OperationType} key + * @param {OperationMap} operation */ static applyOperation(operation) { const tunnelFunction = TUNNELS[operation.type] - // @ts-expect-error: freaks out tunnelFunction(operation) } diff --git a/source/arroost/entities/arrows/connection.js b/source/arroost/entities/arrows/connection.js index 1760cbe..8dff9f1 100644 --- a/source/arroost/entities/arrows/connection.js +++ b/source/arroost/entities/arrows/connection.js @@ -170,6 +170,7 @@ export class ArrowOfConnection extends Entity { const entity = target.entity const dummyWire = new ArrowOfTime({ // @ts-expect-error - Don't know why it isn't figuring out its type here. + // See: https://github.com/microsoft/TypeScript/issues/50651#issuecomment-1476795579 source: sourceEntity, target: entity, timing: ArrowOfConnection.timing, diff --git a/source/arroost/entities/arrows/reality.js b/source/arroost/entities/arrows/reality.js index 3d24b74..fce2660 100644 --- a/source/arroost/entities/arrows/reality.js +++ b/source/arroost/entities/arrows/reality.js @@ -97,7 +97,7 @@ export class ArrowOfReality extends Entity { const operations = [] for (const key in fire) { if (fire[key] === null) continue - // @ts-expect-error: cant be fucked to type Fire correctly + // @ts-expect-error: `Fire` is typed correctly, but `fire` may theoretically be a subtype with additional keys operations.push(...fireCell(shared.nogan, { id, colour: key })) } return operations diff --git a/source/nogan/declare.d.ts b/source/nogan/declare.d.ts index b3426c6..76ec396 100644 --- a/source/nogan/declare.d.ts +++ b/source/nogan/declare.d.ts @@ -95,31 +95,43 @@ declare type Behave = ({ peak: SuccessPeak & { pulse: T } }) => Peak -declare type BehaviourMap = { - [key in PulseType]: Behave> +declare type PulseMap = { + [P in key]: { type: P } & Omit, "type"> +}[key] + +declare type BehaviourMap = { + [P in key]: Behave> } //===========// // Operation // //===========// +// Based on: https://github.com/microsoft/TypeScript/pull/47109 declare type Operation = ReportOperation | InstructionOperation -declare type Operate = (nogan: Nogan, operation: T) => Operation[] -declare type OperationMap = { - [key in OperationType]: Operate> +declare type OperationMap = { + [P in key]: { type: P } & Omit, "type"> +}[key] + +declare type Operate = (nogan: Nogan, operation: OperationMap) => Operation[] +declare type OperateMap = { + [P in key]: Operate

} -declare type TunnelFunction = (operation: T) => void -declare type TunnelMap = { - [key in OperationType]: TunnelFunction> +declare type TunnelFunction = (operation: OperationMap) => void +declare type TunnelMap = { + [P in key]: TunnelFunction

} //======// // Fire // //======// +// declare type Fire = { +// red: Pulse | null +// green: Pulse | null +// blue: Pulse | null +// } declare type Fire = { - red: Pulse | null - green: Pulse | null - blue: Pulse | null + [key in PulseColour]: Pulse | null } //=======// diff --git a/source/nogan/nogan.js b/source/nogan/nogan.js index 75ebbe3..23d43d1 100644 --- a/source/nogan/nogan.js +++ b/source/nogan/nogan.js @@ -1603,13 +1603,12 @@ const getBehavedPeak = ({ nogan, source, target, previous, peak }) => { } /** - * @template {Pulse} T - * @param {T} pulse - * @returns {Behave} + * @template {PulseType} T + * @param {PulseMap} pulse + * @returns {Behave>} */ export const getBehave = (pulse) => { const behave = BEHAVIOURS[pulse.type] - // @ts-expect-error return behave } @@ -1721,12 +1720,11 @@ export const applyOperations = (nogan, { operations }) => { } /** - * @template {Operation} T - * @param {T} operation + * @template {OperationType} T + * @param {OperationMap} operation * @returns {Operate} */ export const getOperate = (operation) => { const operate = OPERATIONS[operation.type] - // @ts-expect-error return operate } diff --git a/source/nogan/operate.js b/source/nogan/operate.js index 1d2857f..dcf2a51 100644 --- a/source/nogan/operate.js +++ b/source/nogan/operate.js @@ -1,6 +1,6 @@ import { getCell, getWire, modifyCell, modifyWire, unfireCell } from "./nogan.js" -/** @type {OperationMap} */ +/** @type {OperateMap} */ export const OPERATIONS = { /** Modify a cell */ modifyCell(nogan, { id, template }) {