Created
August 10, 2016 16:43
-
-
Save newmanbrad/d3c8bc86d9fd1749c65f251f76a72b7d to your computer and use it in GitHub Desktop.
Typescript: Type Annotation Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Example: Type Annotation | |
* | |
* Type annotations remove the need for the compiler to infer the type. | |
* | |
* Available Types: | |
* Boolean: simple true/false value | |
* Number: floating point values | |
* String: textual data | |
* Array: ordered collection of data | |
* Tuple: array with fixed number of elements | |
* Enum: named numeric sets | |
* Any: unsure of value type | |
* Void: no value, the opposite of Any | |
* | |
* | |
* Docs: https://www.typescriptlang.org/docs/handbook/basic-types.html | |
*/ | |
let thisIsAString: string = "This is my string."; | |
thisIsAString = 42; // Error: cannot assign 'string' to a 'number' | |
function createNumber(): number { | |
return 55; | |
} | |
thisIsAString = createNumber(); // Error cannot assign 'string' to a 'number' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment