TypeScript Explained

Apparent types

Apparent types allows compiler to map between variables and their equivalent in type system. By having:

let str = 'I am string';
str.startsWith('I');

TypeScript compiler will know that str (which technically doesn’t have properties) has one of property called startsWith.

Apparent types are not constrained to object types. It is used on object too:

const handleClick = (e: React.MouseEvent) => {
  e.preventDefault(); // telling complier to get apparent type of React.MouseEvent
  submitForm();
};

Based on Widening and Narrowing in TypeScript.

Authors:
  • Krzysztof Żuraw