TypeScript: Conditional Types Explained. Learn what conditionals are and how they are used in TypeScript. Also introduced in the TypeScript handbook, allow us to deterministically define types depending on what parameterised types consist of.
Conditionals in TypeScript, also introduced in the TypeScript handbook, allow us to deterministically define types depending on what parameterised types consist of. The general basic rule is:
type ConditionalType = T extends U ? X : Y
_T_ extends some type
_U_ , then assign
_X_ , otherwise assign
extends keyword is at the heart of conditionals whereby we are checking if every value of
T can be assigned to a value of
T is assignable to
U, then the “true type” will be returned —
X is our case. If
T is not assignable to
U, then the false type,
Y, will be returned.
As a basic example, consider checking whether some type extends a primitive such as a
string, and assign
never if it does not:
type StringOrNot = SomeType extends string ? string : never;
_never_ keyword indicates that a value will never occur — we will cover
_never_ in more detail further down when conditionals are used to coincide with type filtering.
This article covers A-Z about the mobile and web app development process and answers your question on how long does it take to develop/build an app.