Type declaration module
Modules
namespace is almost deprecated. it appeared before ESModule system is accepted.
Instead, Use module system with export/import
(ESModule) or exports, require
(CommonJS)
Why to use module?
Separate and organize the javascript & typescript code
In origin, javascript is used for only browser.
At first, you need to understand Global scope what it is.
Global scope
You must have ever seen <script>
in html , in javascript, you can declare function and use it without separating module, namespace.
- name.js
- printName.js
- printName2.js
name.js
only has name
variable.
// This is global scope
const name = 'vladimir'
console.log('Test1 name : ', name)
console.log('Test2 name : ', name)
In html file, name
is shared all over since it's global scope
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
variable `name` has global scope
<script src="name.js"></script>
<script src="printName.js"></script>
<script src="printName2.js"></script>
</body>
</html>
File list
Result
console output:
If you redeclare name
in another js it must be fail. Suppose redeclare it in printName
printName.js
// redeclaration
const name = 'vladimir'
console.log('Test1 js file name : ', name)
This code error occurs
Uncaught SyntaxError: Identifier 'name' has already been declared (at printName.js:1:1)
How to resolve it?
Use module export/import . It is declaration that "This file is module". not of global scope one.
Module system between Node.js and browser
Node.js uses CommonJS from starting with require/exports
.
However, the browser doesn't support CommonJS module. At first, use <script>
(standard script) without module system.
and now Support modern module system with import/export
Let's see how to use it.
HTML Tag
<body>
<script type="module" src="./dist/index.js"></script>
</body>
But maybe meet errors since there's CORS policy of browser.
Get file:///Users~~~/dist/index.js net::ERR_FAILED
You need to pay attention to local testing
if you try to load the HTML file locally (i.e. with a file:// URL),
you'll run into CORS errors due to JavaScript module security requirements.
- MDN docs
tsconfig.json
{
"module": "ES6"
}
This means that We're serving anything and trying to use file protocol.
# From this, html file served from the server (HTTP) instead of `file`
$ yarn install lite-server
$ yarn start
Use modern ES Module using tsconfig and script with module
property and server.
Or use bundle library like browserify
import 'type' only
- Just
import type
this is helpful when I want to refer to just type not the module with actual code. - Reduce the size of compiled file since it must be removed completely when transpiling.
Example
- types/Item.ts
- itemList.ts
export type Item = 'HP' | 'MP'
export class ItemKing {
name: string
}
Can't be used for extension when to use import type
import {Item} from "./types/Item";
// highlight-the-nextline
import type {ItemKing} from './types/Item'
const itemList : Item[] = []
// Error occurs 🚫
// highlight-the-nextline
class SecondItemKing extends ItemKing{
}