Skip to content

Instantly share code, notes, and snippets.

@alenvlahovljak
Last active September 6, 2020 09:09
Show Gist options
  • Save alenvlahovljak/db8cb937f2b9491125a737503c5e211b to your computer and use it in GitHub Desktop.
Save alenvlahovljak/db8cb937f2b9491125a737503c5e211b to your computer and use it in GitHub Desktop.
How to adopt a static typing?
//* Type Inference *//
// The system can guess that array will contain only numbers
// based on the array's initialization.
const arr1 = [14, 21, 28];
arr1.push(15);
arr1.push("Hello World!");
// Type Inference is going to set the type to 'any'
// because we initialize an empty array
const arr2 = [];
arr2.push(27);
arr2.push("Hi there!");
// Type Annotation is a must in this case
let arr3: number[];
arr3.push(77);
arr3.push("It's a string!");
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment