TypeScript


Docs

https://www.typescriptlang.org/docs/handbook/variable-declarations.html https://www.typescriptlang.org/docs/handbook/compiler-options.html


What's tsconfig.json ?

It specifies the root files and the compiler options required to compile the project.

https://www.typescriptlang.org/docs/handbook/tsconfig-json.html

https://www.typescriptlang.org/tsconfig/


Can TypeScript work with React and Webpack ?

Yes. Here is some docs.

https://webpack.js.org/guides/typescript/

https://github.com/typescript-cheatsheets/react

https://www.sitepoint.com/react-with-typescript-best-practices/


Can you give an example of tsconfig.json and explain it ?

Here you go. Explanations in comments.

tsconfig.json example 1

tsconfig.json example 2


Terminal

Command to transpile

Command to run script — mind the ".js" , not ".ts"


Variables

Type inference: TypeScript expects the data type of the variable to match the type of the value initially assigned to it at its declaration.

  • Inference = Deduction

  • Infer = Deduct

TypeScript recognizes JavaScript’s built-in “primitive” data types:

  • boolean

  • number

  • null

  • string

  • undefined

In situations where it isn’t able to infer a type, TypeScript will consider a variable to be of type "any".


Type In Functions Params


Tuples and Spread Syntax


type annotations


Tuples

In TypeScript, when an array is typed with elements of specific types, it’s called a tuple

Tuples and arrays do not have compatible types within TypeScript We can’t assign an array to a tuple variable, even when the elements are of the correct type


type annotations on functions


Functions default parameters


question mark

To indicate that a parameter is intentionally optional, we add a ? after its name. This tells TypeScript that the parameter is allowed to be undefined and doesn’t always have to be provided.

TypeScript infers the type of an initialized variable to be the same as the type of its initial value.

Same goes for functions.

TypeScript will infer the variable type to be the same as the default value’s type.


type annotations + bang operator (exclamation mark)

Exclamation mark operator aka bang operator. It is the non-null assertion operator. It is a way to tell the compiler "this expression cannot be null or undefined here, so don't complain about the possibility of it being null or undefined."

Can be used in other parts of your code

https://stackoverflow.com/questions/42273853/in-typescript-what-is-the-exclamation-mark-bang-operator-when-dereferenci


tsconfig.json file

Always placed in the root of your project and you can customize what rules you want the TypeScript compiler to enforce.

"strictNullChecks" —> variables can only have null or undefined values if they are explicitly assigned those values.

"include" —> what files the compiler applies the rules to. ["**/*.ts"] —> the compiler should check every single file that has a .ts extension.


Enums

TypeScript automatically assigns: ExampleEnum.FirstValue = 0, ExampleEnum.SecondValue = 1, and ExampleEnum.ThirdValue = 2.


Enums and Tuples


String Enums

We recommend to always use string enums because numeric enums allow for some behaviors that can let bugs sneak into our code. With string enums, variables cannot be assigned to strings at all!


String Enums continued


Enums


Type Aliases


Type Aliases


Function Types


Generic Types

Allows us to use T within the type annotation as a type placeholder.

Later, when the generic type is used, T is replaced with the provided type.


Generic Functions


Union Types


Type Narrowing


Type Unions and Arrays

Annotation: (type | type)[]


Unions with Literal Types


"in" and Type Guards


Interfaces and Types


Interfaces vs Types: Principles

interface may only type objects.

type can type objects, primitives, and more.

interface —> when we want our types to be constrained, so we’re more likely to write consistent code —> perfect match for writing object-oriented programs because these programs need many typed objects —> interface and class are a great match


Interfaces vs Types: How To


Composed Types


Extend Interfaces


Index Signatures: Problem Statement

When typing objects in TypeScript, sometimes it’s not possible to know the property names for an object. i.e. when we get back information from an outside data source/API

We still want to be able to type our objects. Dynamically?


Index Signatures

In the [latitude: string] syntax, the latitude name is purely for us, the developer. As a human-readable name that will show up in potential error messages later.


Optional Type Members: Problem Statement

We wanna make some type members optional.

Last updated