Knowledgebase

Basic Types and Annotations Print

  • webdevelopment, guide, howto, solution, zillionkinghost, hosting, support
  • 0

Declaring what things are.

WHAT CAN BE ANNOTATED

Variables Function parameters Return values Object shapes

WHAT THE BASIC TYPES ARE

Text, numbers and boolean values Arrays of a given type Objects with defined properties The absence of a value, in its two forms A type meaning anything

WHAT TO AVOID

That last one.

WHY

It disables checking entirely, which defeats the purpose.

WHAT TO USE INSTEAD WHEN THE TYPE IS GENUINELY UNKNOWN

The type requiring you to narrow it before use.

WHY

It forces a check, rather than allowing assumptions.

WHAT INFERENCE DOES

Determines types automatically from assignment.

WHAT THAT MEANS

Annotating everything is unnecessary.

WHAT TO ANNOTATE EXPLICITLY

Function parameters and return types Anything where inference produces something too broad

WHY FUNCTION SIGNATURES

They are the contract, and they document intent.

WHAT UNION TYPES PROVIDE

A value that may be one of several types.

WHAT THAT REQUIRES

Narrowing before use, which the compiler enforces.


Was this answer helpful?
Back

Are you happy with your experience? Leave us a review on Trustpilot.


Trustpilot