Skip to content

Wondering how do we need to create a type definition for an existing javascript file?

Notifications You must be signed in to change notification settings

rodrigoelp/reactnative-typescript-exercise-5

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Dealing with type declarations

Typescript has several aspects making it a superior programming language to plain javascript... but browsing around I have found several libraries that look pretty promising and I am not sure if those are already included into the defined types for typescript.

One example of these libraries that is not defined (yet) is react-native-navigation. Thankfully, the main react-navigation library is well supported and easier to include into your project than react-native-navigation.

So, before I find one of those libraries without its types defined; I've decided to learn how to generate the type definition.

What do we have here?

The ./lib/ folder contains two javascript files { geometry.js, people.js } containing some definitions that we would like to use in our application app.ts.

At the moment, the file sampletest.js has all the functionality that I want to get to work in the ./src/app.ts file. hopefully the libraries will be exported into a people.d.ts and geometry.d.ts files that will work with my app.

I started by reading typescript's docos, then browsed a few examples to understand what I need to do.

Once done, I've identified at least two different ways that I need to tackle first:

// object definition
var objectName = (function(){
    constructor()  { }
    return objectName;
})();

exports.objectName = objectName;

Or

var moduleName = {
    objectName: (objectDefinitionAsAbove)(),
    functionName(args) { body },
}

exports.moduleName = moduleName;

Let's see how these go.

Comments after writing the exercise

Some declarations and its usage was straight forward. The module declaration was sort of weird and I am still trying to find a better way of dealing with this.

My initial script was sampletest.js, which gave a framework to get the library into a shape that I wanted to work with.

From there I started generating people.d.ts and geometry.d.ts which I had to include with my original people.js and geometry.js otherwise it was not working... But end up with the following questionsn that I need to do to an expert while I am learning:

  1. Is there a way to generate the definition files and locate it in a different folder to the library?
  2. What's the best way to export types included in a module?
  3. What other types should I practice exporting?

BTW!!!!

I did not include the output/app.js generated file from the src/app.tsx. If you want to check this works, remember to compile the code and then run node with the output.

How to run this code?

# Clone it
git clone git@github.com:rodrigoelp/reactnative-typescript-exercise-5.git declarations
# Get into that folder
cd declarations/
# install dependencies
yarn
# Compile your code.
./node_modules/.bin/tsc
# Run it!
node output/app.js

About

Wondering how do we need to create a type definition for an existing javascript file?

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published