Typescript
Last updated
Last updated
Describe advantages and disadvantages to using TypeScript
Identify and use basic types, interfaces and additional TypeScript stuff
Understand type inference and declaration
Configure and use the typescript compiler
Identifying bugs at compile time is better than finding them at runtime
Type enforcement in large code bases reduces bugs across the organization/teams/time
TypeScript allows ESNext syntax -- though many of the features highlighted by TS folks have been introduced with ES6 and 7
Lowish barrier to entry
can use it sparingly to start (your JS is probably fine, just add some typings or any
)
implicit and explicit typing
This is a pretty great resource put together by some benevolent dev:
TypeScript Deep Dive by @basarat
Adds complexity to your project
directory structure needs source and build
setup compiler or babel and/or webpack and build step
TS compiler will yell at you for things that you have perceived as legal for the entirety of your JS career
Advanced techniques can be a little confusing
Install TypeScript and its compiler on your local machine!
We're all set!
Let's test it out
type_fun.ts
Save your type_fun.ts
and run this bash command:
You should now see two files, type_fun.ts
and type_fun.js
! The tsc
command compiles our typescript file into javascript!
We can get started with TypeScript by adding just a little extra cruft to the JS syntax we know and love. By now, you are all very familiar with the javascript primitives: string
, number
, boolean
. TypeScript makes use of these primitives... and then adds to it!
In order to apply type constraints to our variables with TypeScript, all we need to do is declare a type
after the variable name, separated by a colon
String
Your run of the mill string type
Number
Can be used with decimal integers and floats as well as hex, octal and binary numbers
Boolean
our old friends true
and false
Any
oh my! you're telling me I don't actually have to plan ahead?
Array
lets add primitive typings to arrays (syntax may vary!)
void
used for functions that do not return a value
null
null
, this is one of Typescript's two subtypes
undefined
undefined
, Typescript's other subtype
Object
anything that is not number
, string
, boolean
, null
, or undefined
never
represents the type of values that never occur
Tuple
enforced typings on a specified number elements
Enum
Enforce a set of values -- we can use custom Type
s in many cases
string
number
boolean
any
When a data type is not known or required ahead of time, any
can be used.
However, if we know that a variable can accept both strings or numbers, TypeScript allows us to plan for this scenario with the |
operator. (This is called a "Union" type and can be a more advanced technique)
Typescript gives us two subtypes, null
and undefined
. These are used when we've defined the type of a variable, but we don't know what that value is yet.
TypeScript offers 2 options for enforcing type constraints on an array. 1. Adding []
after a type declaration 2. Using angle brackets and the Array
generic type
Be careful when using the angle bracket notation as it can cause conflicts when working with TSX, the TypeScript equivalent of JSX.
The format we used to add type constraints to our functions and methods is as follows:
If our function will not be returning anything, we can assign the return type void
.
All this extra syntax making your headspin?
The typescript compiler can infer some of your typings from the initial definition!
In essence, typescript looks at a variable that isn't typed (in this case otherStr
) and says, "This looks like a string, follows the sytax of a string, must be a string!"
This will be more apparent and relevant when we get into interfaces and enforcing object type structures in the next section.
The rules that govern these type inferences can vary and/or be configured by the compiler. In general, if the data type is not primitive, it is unlikely that implicit typping will work.
For more information: Rules of Type Inference