Used normally to give a new name for existing type or construct a new type.
type ContactAddress = string
it is actual string type but has different name.
other useful uses can be:
type ContactAddress = string | Date
: says it can be one type of string, Data.type ContactAddress = Contact & Address
: combine two types into one type.type status = 'active' | 'inactive'
: can be used like enum to define restricted values.
|
directly like function print(a: string | number)
if (typeof contact.birthDate === "number")
const user = {name:"mahmoud" , age:15};
function printUser(params: typeof user) {
}
// note: typeof can not be used with type like User, Contact it needs actual object/value.
retrive properties/keys as union like type title = keyof User
so it can be used later with something like indexed access types.
Get type by access property like:
{
contactId: Contact["id"];
}
contactId
here will be of type same as property "id" of Contact
.
In JS we can use modules to organize our code and export what we need then import it where needed.
for exporting
// let say file name is util.js
export function doSomeThing(){
}
for importing
// let say file name is app.js
import { doSomeThing } from "./util.js"
doSomeThing();
when use it in browser we will need only reference app.js
:
<script type="module" src="app.js"></script>
you can define global.d.ts
to define methods you want to export between modules and get support of typescript [this only make definition for exported methods]
declare global {
// that doSomeThing
function dodoSomeThing():string
}
export {}
class Customer{
}
interface Customer {
save():void
}
const customer = new Customer();
customer.save = function(){
// do what yuo want
}
global.d.ts
that have interface with name Window
then add what you want.declare global {
interface Window {
// value of something
myValue:string
}
}
export {}
to use exports with ts you will need to add "module": "CommonJS"
to compilerOptions
this for node application
for web app you will need to use bundle apps like webpack
, parcel
.