Fundamentals Flashcards
What is TypeScript
?
TypeScript is a superset of JavaScript, In other words, all Javascript programs are already TypeScript programs. TypeScript has some syntax of its own, so TypeScript programs are not, in general, valid JavaScript programs.
What does TypeScript
compile to?
JavaScript
What is a type system
?
A type system
is a set of rules that a typechecker uses to assign types to your
program.
There are generally two kinds of type systems: type systems in which you have to tell
the compiler what type everything is with explicit syntax, and type systems that infer
the types of things for you automatically.
TypeScript is inspired by both kinds of type systems: you can explicitly annotate your
types, or you can let TypeScript infer most of them for you.
Boris Cherny, Programming TypeScript - Making Your JavaScript Applications Scale (O’Reilly 2019), p. 17
What is type inference
?
TypeScript knows the JavaScript language and will generate types for you in many cases.
For example in creating a variable and assigning it to a particular value, TypeScript will use the value as its type.
let helloWorld = "Hello World"; // <== Infered type is `string`
Source typescriptlang
What happens when TypeScript can not infer a type?
It will assign the type any
.
This is dangerous because variables with type any
are excluded from any kind of type checking and this can in some cases extend to the logic that uses such variables.
What is a JS SyntaxError
?
The SyntaxError
object represents an error when trying to interpret syntactically invalid code. It is thrown when the JavaScript engine encounters tokens or token order that does not conform to the syntax of the language when parsing code.
Source MDN
What is a JS TypeError
?
The TypeError
object represents an error when an operation could not be performed, typically (but not exclusively) when a value is not of the expected type.
Source MDN
When does JS throw SyntaxError
and TypeError
errors?
At runtime and at compile time
When does TS throw the equivalent to JS SyntaxError
and TypeError
errors?
TypeScript throws both syntax-related
errors and type-related
errors at compile time.
This type of errors are also known as non-exception failures.
Source typescriptlang
What is a Structural Type System
?
One of TypeScript’s core principles is that type checking focuses on the shape that values have. This is sometimes called “duck typing” or “structural typing”.
In a structural type system, if two objects have the same shape, they are considered to be of the same type.
interface Point { x: number; y: number; } function logPoint(p: Point) { console.log(`${p.x}, ${p.y}`); } // logs "12, 26" const point = { x: 12, y: 26 }; logPoint(point);
The point variable is never declared to be a Point type. However, TypeScript compares the shape of point to the shape of Point in the type-check. They have the same shape, so the code passes.
The shape-matching only requires a subset of the object’s fields to match.
Source typescriptlang
What are type annotations
?
Type annotations in TypeScript are lightweight ways to record the intended contract of the function or variable.
The syntax for a type annotation is to put a colon (:
) followed by the type after the variable name before any assignment.
Example:
function greeter(person: string) { return "Hello, " + person; } let user: number[] = [0, 1, 2];
Source typescriptlang
What is Type inference
?
In TypeScript, there are several places where type inference is used to provide type information when there is no explicit type annotation. For example, in this code
let x = 3; // Type is number
The type of the x
variable is inferred to be number
. This kind of inference takes place when initializing variables and members, setting parameter default values, and determining function return types.
Source typescriptlang