How to cast TypeScript type in JavaScript using JSDoc
14:35 24 Nov 2021

When using TypeScript to check JavaScript code, how do you cast to a different type than is inferred? TypeScript has the and as Type syntax but these aren't valid in JavaScript.

/**
 * @param {{id: string, value: number}} details - object with required properties
 */
function stringifyObject(details) {
  return `${details.id}: ${details.value}`;
}

const testValue = {id: 'no-value'};
stringifyObject(testValue); // want to cast testValue to ignore missing property
stringifyObject(/** @type {any} */ testValue); // this doesn't work
typescript jsdoc