TypeScript 4.5, which is currently a release candidate, includes an exciting new feature for us types in js JSDoc users:
Defaults for template tags, microsoft/TypeScript#45483: @template [T=string]
I found this through when looking for a way to void T
defaulting to any
when no value was given in this code:
/** @template [T=undefined] */ class ErrorWithCause extends Error { /** * @param {string} message * @param {{ cause?: T }} [options] */ constructor (message, { cause } = {}) { if (cause) { /** @type {T} */ this.cause = cause; } // ... } }
Now it gets set to undefined
when no value is provied and my type-coverage
got closer to 100%.
And it properly compiles to:
export class ErrorWithCause<T = undefined> extends Error { constructor(message: string, { cause }?: { cause?: T; } | undefined); cause: T; }
I will push this new code to my pony-cause
module very soon, just wanted to write this up first 🥳
Originally posted as a discussion in the types-in-js community:
Top comments (0)