Typescript: A resource to better grok the lack of errors when using types, interfaces, unions, intersections.

Created on 11 May 2017  Β·  2Comments  Β·  Source: microsoft/TypeScript

Are there simple guide phrases / mnemonics people to use to remind themselves a type is not going to do be as restrictive as what they think it will be? Like the is-a / has-a for helping choosing classes or interfaces?

I always expect more assistance from my typings than they actually afford, for example, and "Why does TypeScript not raise a warning when an interface is overridden", and "Typescript type for limiting assignment". This has led me to keep typings as simple as possible for now and rely more on tests to catch type errors. It's not a complaint. I think the TypeScript team are doing an excellent job of building a type system for Javascript that isn't cripplingly pedantic or onerous to use and allows the expression of an incredibly power ("dangerously" so at times) language. But I was hoping someone might point towards some resource for understanding types better. I know this is a shifting landscape too so perhaps this isn't possible until things settle and Typescript sees improvements less frequently.

This is a question. I tried asking it on stackoverflow and it was closed (not unreasonably).

Question

Most helpful comment

Some mnemonics I just made up:

  • If your type is all-optional, you're likely to fall
  • An unused generic is poison like arsenic
  • Identical classes are stubborn like asses
  • A Fish & Car can both swim and drive far, but a (need union type rhyme here)
  • Type aliases look nominal but are only a new label

All 2 comments

Some mnemonics I just made up:

  • If your type is all-optional, you're likely to fall
  • An unused generic is poison like arsenic
  • Identical classes are stubborn like asses
  • A Fish & Car can both swim and drive far, but a (need union type rhyme here)
  • Type aliases look nominal but are only a new label

That's excellent thanks Ryan. I am closing this but don't let it put anyone off adding / editing πŸ‘

There's something in this example that needs to be extracted from this union but union is rhyme poor! "Fish & Car can both ... and ..." has us covered along way though.

type MaybeOneOrTheOther = ({
    first: number;
    orTheOther?: undefined;
} | {
    first?: undefined;
    orTheOther: string;
} | {
    first?: undefined;
    orTheOther?: undefined;
});

var result: MaybeOneOrTheOther = {};           // βœ“ valid
result = { first: 1 };                         // βœ“ valid
result = { orTheOther: "other" };              // βœ“ valid
result = { first: 1, orTheOther: "other" };    // βœ“ error - both defined
result = { first: 1, orTheOther: 3 };          // βœ“ error - one defined correctly, the other wrong type
result = { first: 1, unknown: "bad" };         // βœ“ error - one defined correctly, additional unknown also provided

"If your type is all-optional, you're likely to fall" so favour something: string | undefined over something?: string;

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Antony-Jones picture Antony-Jones  Β·  3Comments

weswigham picture weswigham  Β·  3Comments

wmaurer picture wmaurer  Β·  3Comments

fwanicka picture fwanicka  Β·  3Comments

jbondc picture jbondc  Β·  3Comments