I installed @types/[email protected], then in tsconfig.json I added jquery.cookie in types section. Visual Studio Code shows that $.cookie is available to use, but when I run my code I get error in console that $.cookie() is not a function. Where is the problem? Am I missing something? Should I reference it somewhere else?
1 Answer
Have you included the jquery.cookie package in your code? Or just the @types/[email protected]?
@types are just definition files for TypeScript, not the actual code it self. So we still need to install the code it self:
npm install --save jquery.cookie
Then add it to your packaging, for instance for SystemJS:
SystemJS.config({
'map': {
'jquery.cookie': 'npm:jquery.cookie'
},
'paths': {
'npm:': 'node_modules/'
}
});
To sum up:
@typesare definitions to allow TypeScript understand code/packages written in JavaScript (as most of them are or at least compiled to).@typesshouldn't be imported in the code it self. Only installed, the TypeScript compiler automatically looks for all defintions innode_modules/@types.- The package you want to use with TypeScript still needs to be installed with
npm(oryarn). These hold the actual code.
2 Comments
nick_green
I tried to import it in app.module.ts like this: "import '@types/jquery.cookie';" but when I compile code it throws error that couldn't find module.
nick_green
Thanks for your answer! :)