Skip to content

Instantly share code, notes, and snippets.

@NoriSte
Last active October 14, 2021 05:50
Show Gist options
  • Save NoriSte/f15e474993cdb95921432d2ed4d2badd to your computer and use it in GitHub Desktop.
Save NoriSte/f15e474993cdb95921432d2ed4d2badd to your computer and use it in GitHub Desktop.
TS trap for corrupted *.d.ts files

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...

/**
* 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