String in Typescript - simple explanation with examples
Arika O
Posted on May 16, 2020
Just like number
, the string
type is pretty straight forward: it specifies that we want something to be a string and a string only. It's used in the same manner:
let firstName: string = "Allan";
let lastName: string = "Korb";
If we try to misuse it, and store a boolean inside of a variable of type number, for example, the editor will complain:
A super simple situation in which we could use this is when trying to print some of the properties of an object (let's say we got it back from the server after making an API call). We're trying to print a name made out of two strings but we use by mistake a property that stores a number.
Image source: Brett Jordan/ @brett_jordan Unsplash
π πͺ π
π©
Arika O
Posted on May 16, 2020
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.