5-5 1: What is the module, different formats of module technique, import, and export
The module is used to control the scope of the variable, function, class, etc. declaration in Typescript just like JavaScript.
By setting "module": "commonjs"
, "module": "ES6"
, "module": "ES2015"
in the tsconfig.js
file we can control our Typescript code compilation to Javascript versions.
5-5 2: Different types of Import and export statement
-
using alias
import { *adder* as addTwo } from "./module";
-
Wildcard import
import * as Methods from "./module";
*
means everything. Meaning importing everything from the file- usage -
Methods.myFunction()
-
default
export- front another file,
export default avg;
then use it like,import avg, { multiply } from "./module";
, call itavg()
- front another file,
-
default
export using objectexport default { adder, subtract, multiply, divide, avg };
import Jekono from "./module";
Jekono.adder(3, 4);
-
taking the programs to different files then
export default
and then using like this -import addTwo from "./utils/adder"; // can name anything cause of default export import avg from "./utils/avg"; import divide from "./utils/divide"; import subtract from "./utils/subtract";
-
re-export
- When there are a lot of imports.
- creating
utils
folder, creating the utility functions then creatingindex.js
file and importing from there.
5-5 3: Know About utility Types
Utility types are used for manipulating an existing type/interface.
-
Pick
- creates a new type by picking a single or multiple properties
key
(string or union of strings) from another type. - opposite of
Omit
.
- creates a new type by picking a single or multiple properties
-
Omit
- creates a new type by omitting/removing single or multiple properties from another type.
- opposite of
Pick
.
-
Partial
- creates a new type by making all the properties optional of an existing type.
- opposite of
Required
.
-
Required
- creates a new type by making all the properties required of an existing type.
- opposite of
Partial
.
-
Readonly
- creates a new type by making all the properties readonly of an existing type. Just like
readonly
access modifiers.
- creates a new type by making all the properties readonly of an existing type. Just like
-
Record
Record
is a shortcut to defining an object type with a specific key type and value type.- Index signatures can’t be used with literal types
so we use
Record
to do thistype MyObj = Record<"a" | "b" | "c", string>;
5-5 4: How to use multiple interfaces as a type and create a model for a class