Typescript: Ambient declaration, definitely types, declaration file

hipster' Santos
2 min readDec 24, 2019

--

I think everyone struggling to understand these topics, I’ll start it by supplying their concepts which’ll make simpler to everyone understand it.

The main purpose here is to deal with vanilla JavaScript and then turn it into typescript code or making the original JavaScript definition running in typescript without typing errors, and in order to do that ,you gotta allowing for the contents being approached right here.

Ambient declaration: is more used to add type either information for your existing JavaScript code or to your third-party library in order to consume in your typescript program.

Maybe you want to consume your own js library (not-typescript) in order to do that you gotta wrap it into typescript by adding environment declarations, however using declare keyword, it will prevent all items wrapped in runtime.

further I’ll be showing by examples.

Environment declaration for express

declare class Body{

public params:Array<Object>;

}

declare interface request{

body:Body

}

declare interface response{

send:()=>void;

status:(errorkind:string)=> void;

}

declare interface express{

get:(uri:string,callback:(req:request,res:response)=>void )=>void

post:(uri:string,callback:(req:request,res:response)=>void )=>void

put:(uri:string,callback:(req:request,res:response)=>void )=>void

delete:(uri:string,callback:(req:request,res:response)=>void )=>void

}

declare class Express implements express{

public uri:string;

get:(uri:string,callback:(req:request,res:response)=>void )=>void

post:(uri:string,callback:(req:request,res:response)=>void )=>void

put:(uri:string,callback:(req:request,res:response)=>void )=>void

delete:(uri:string,callback:(req:request,res:response)=>void )=>void

}

var app = new Express();

app.get(‘api/typings’,(req:request,res:response)=>{

res.status(‘404’);

req.body.params.filter( v=> v.name);

});

Declarations simply means: notify compiler that an external variable will end up existing at runtime without supplying anymore further detail of the structure of the external variable. When you state your type definition, you just saying to compiler only get a grasp of the type information, no implement it at runtime only get a grasp and then generate the proper javascript file allowing for the prior grasp. Because the whole code blocking stated using declare keyword will be erased at compiling time.

The main idea behind it, is to ensure that the existing typescript coding will match up to Javascript output. By allowing for the prior information, that every declared item by using declare keyword will be erased at runtime.

Declaration file: foster the placement of your ambient declaration into a file, follow certain name convention like. d.ts for the referring file extension. Don’t miss the declare keyword.

Definitely typed: it’s a well-known project by community when it comes to third-party ambient declaration. instead of handwrite your own one, you first can check it out on the community by community I’m saying this sort of project http://definitelytyped.org/

I hopefully it helps you.

By Hipster Santos

Twitter- @santosfefe4

Facebook: facebook.com/santos.fefe

--

--

hipster' Santos
hipster' Santos

Written by hipster' Santos

Fullstack developer , Distributed system engineer,Competitive programmer find at https://github.com/HipsterSantos

No responses yet