I had to be creative with TypeScript to prevent a common problem in our codebase, where we can't validate our *.d.ts files through skipLibCheck: false
and a type (Action
) sometimes become any
...
Last active
October 14, 2021 05:50
-
-
Save NoriSte/f15e474993cdb95921432d2ed4d2badd to your computer and use it in GitHub Desktop.
TS trap for corrupted *.d.ts files
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This is a TypeScript trap that ensures Action is not corrupted. | |
* | |
* The problem: since we can't leverage TS lib check, we are completely blind about the validity of | |
* the type of the actions. A slight oversight in the d.ts files results in Action being `any` without | |
* us noticing it. | |
* | |
* The solution: this harmless function lets TS throw in case of mistakes in the d.ts files with the | |
* following error | |
* | |
* > Type 'boolean' is not assignable to type 'never'. | |
* | |
* as a result, the Developer introducing the regression is instantaneously prompted, protecting us. | |
*/ | |
type ActionTypeIsValid = any extends uui.domain.actions.Action ? never : true | |
export function ensureActionTypeIsValid(): ActionTypeIsValid { | |
// If TS throws, chances are you removed/renamed an action incorrectly | |
return true | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment