TypeScript Tips
Typing
When creating types it’s good to use interfaces instead of classes. Difference is that interface is not going to be transpiled to JavaScript, which results in a shorter JS code.
Example:
export interface IActivity { id: string; title: string; description: string; category: string; date: Date; city: string; venue: string;}
Properties Declaration
We can quickly declare some class properties and make them settable via a constructor like this:
export class Ingredient { constructor(public name: string, public amount: number) {}}
never type
There’s a never
type. It’s a bit similar to void
in the sense that the user is not expected to use the value of that type. The distintion is:
- functions that return
void
actually returnundefined
- functions that return
never
are programmed in a way that they never actually return. They could always throw some exception or they might be just infinite.