I'm transforming a small express api to use TypeScript, but some of the packages don't have a #types/ so when I import them I get a TS2307 error.
Is there something I can do to resolve the error? Or maybe type it myself, depending on the package complexity. One example is express-bearer-token(found here)
The quick way is to create a globals.d.ts file and add the line:
declare module "express-bearer-token";
This will treat express-bearer-token as a JS module without typings. More information about that here.
From there, you can start adding more typings yourself if you wish. You can find some information about writing your own definitions here.
For daisyui
Create global.d.ts in the root directory
Add the below line
declare module "daisyui";
Note: Stop the server and re-start if you the server is running already.
Related
This is my first npm module, so be kind to me. The repository is available here. The package content is available on folder src. My question is about functionality export.
During research, this fellow provides a great step-by-step tutorial to create such package. At step 2, it exports its functionalities from multiple files into index.js and modify them in such a way that he may export only with ONE default name (neat!).
As you can see in my case, there are several functions to export.
How can I make it in a proper and clean way? The current way outputs the following error:
SyntaxError: ./arqeo/src/index.js: Identifier 'isArtifact' has already been declared. (15:11)
I know that this is not best practice but I dont have a choice. The project I am working on has had its own moment typings file for a long time and it is now causing issues. Moment now provides its own typing file which I want to use, but I need to keep it global. How do I do this please?
I have tried just editing our moment.d.ts file inline with suggestions here
[TypeScript: How can I make an existing namespace global? which didnt work with an error of 'moment refers to a UMD global, but the current file is a module'.
I have also tried editing the typescript config file to simply 'include' the the node module d.ts file but this gave me a no inputs were found in config file error.
Solved this by creating a new global custom typing and a global value cosponsoring which is a separate moment instance. Super hacky and very temporary but works for now...
I try to load this module in my typescript add.
First I added the npm package and the module has been installed correctly in my node_modules folder as simpl-schema.
Since there are no typings for this package I added this line:
declare var SimpleSchema: any;
I tried to import the package with import * as SimpleSchema from 'simpl-schema'; and got the message Cannot find module 'simpl-schema'. I think a got this since simpl-schema doesn't contain type information but I'm not sure.
I found tons of questions regarding this topic here, on reddit and other forums with a lot of suggestions which doesn't work for my setup. So I'm wondering whats the right way to this.
You are correct that you're getting Cannot find module 'simpl-schema' because types are either not available, or types are not setup properly.
Using declare var SimpleSchema: any; in your ts says that SimpleSchema is a variable (var) of type any declared outside the scope of that file. The above will not impact the result of importing simpl-schema directly. An example would be adding something like declare const window: any; to get access to the window object, if it weren't already defined for you, elsewhere.
See here for a way to leverage the any type when looking to import modules that don't have types available.
I'm trying to use a session helper called connect-session-knex which is obscure enough that it does not have a published typescript definition. So when I try to compile my typescript node project, I get the error,
error TS2307 Cannot find module 'connect-session-knex'
Is there a way to ignore TS for this module only? How do I import it without the TSD? I know knex has a tsd, but the wrapper does not. I'm asking this from a generic standpoint of what to do with libraries without type definitions.
For anyone looking: Compiling typescript when it does not have tsd. Missing tsd. Without tsd.
error TS2307 Cannot find module 'connect-session-knex'
Is there a way to ignore TS for this module only? How do I import it without the TSD?
Use var/require instead of import/require. i.e.
var csk = require('connect-session-knex');
Note you should have node.d.ts included for require to be declared.
Also : https://basarat.gitbooks.io/typescript/content/docs/node/nodejs.html
Another suggestion is to start you own .d.ts file as an empty definition file and export the module. Then if you want to get intellisense on the module you can add definitions to it.
e.g. connect-session-knex.d.ts:
// declare module
declare module "connect-session-knex" {
}
I think it would be great to directly import modules from node_modules directory without need to manually provide a declaration file for it (let us assume that declaration is provided with module itself). I guess that problem are dependencies that come with declarations (file paths could be resolved relative to the module, but that would cause duplicates and compiler can't handle that).
Currently working with node modules is very inconvenient because simple install from npm repository just isn't enough and we have to manually search for declarations and provide them in our project. Let's say that our project is dependent on 10 node modules (all of them have declarations) and after a year we would like to update them. We would have to manually search for new declarations and let's say that we have around 20 projects like this (it would become a nightmare). Maybe there should be an option to directly import .ts file from node module?
Do you have any suggestions?
This is a re-post from CodePlex to hear your opinions ...
If you use grunt-typescript then I've got a pull request which solves this at least for me. See https://github.com/k-maru/grunt-typescript/pull/36
From the pull request README
Working with modules in node_modules (i.e. npm)
The standard way to use npm packages is to provide a definition file
that specifies the package to the typescript and import the module from there.
///<reference path="path/to/mod.d.ts" />
import mod = module('mod')
The typescript compiler will convert the import to a nodejs require.
var mod = require('mod')
This is pretty unwieldy as you need to know the precise path to the
npm installed package and npm can put the package at pretty much any
level when you are working with multiple levels of dependencies.
With the node_modules option in the grunt config you can just
import a npm package without need to know the exact level where the
package has been installed by npm as long as it is installed locally
and not globally.
To import an npm module in your typescript source do
import npmModule = module('node_modules/npmModule/foo')
Mostly due to a lucky chance this works. Typescript compiler will read
the typescript definition file node_modules/npmModule/foo.d.ts if it
is present at some point on the way towards the root and the resulting
javascript file will contain a require for npmModule/foo if needed.
I don't think that node modules will ever contain built-in typescript support. The language still is a 0.x release and officially described as an alpha version.
Nevertheless there are means to ease the configuration process for typescript. Github already contains huge collections of .d.ts files such as:
https://github.com/borisyankov/DefinitelyTyped
or
https://github.com/soywiz/typescript-node-definitions
You might want to take a look at this tool: https://github.com/Diullei/tsd .
I've never used it but it seems like it's almost what you're looking for.
Moreover I've heard that an official database of .d.ts files is planned. Unfortunately I couldn't find the link but it will probably be some time before this is implemented anyways.