Item 67: Export All Types That Appear in Public APIs

May 10, 2024 ยท View on GitHub

Things to Remember

  • Export types that appear in any form in any public method. Your users will be able to extract them anyway, so you may as well make it easy for them.

Code Samples

interface SecretName {
  first: string;
  last: string;
}

interface SecretSanta {
  name: SecretName;
  gift: string;
}

export function getGift(name: SecretName, gift: string): SecretSanta {
  // ...
}

๐Ÿ’ป playground


type MySanta = ReturnType<typeof getGift>;
//   ^? type MySanta = SecretSanta
type MyName = Parameters<typeof getGift>[0];
//   ^? type MyName = SecretName

๐Ÿ’ป playground