carhartl / typescript-cookie Goto Github PK
View Code? Open in Web Editor NEWThis project forked from js-cookie/js-cookie
A simple, lightweight TypeScript API for handling browser cookies
License: MIT License
This project forked from js-cookie/js-cookie
A simple, lightweight TypeScript API for handling browser cookies
License: MIT License
Is your feature request related to a problem? Please describe.
Would love to be able to add types to getCookie
when I know what to expect
Describe the solution you'd like
Add generics to the signature: getCookie<KnownType>()
Describe alternatives you've considered
getCookie() as KnownType | undefined
Additional context
Not sure I'm up to it, but would be open to submitting a PR!
Describe the bug
The library works great when running my application, but fails to import when running tests with Jest. Tests fails with the error message:
Cannot find module 'typescript-cookie' from 'path/to/my/file.tsx'
I found others with the same problem on this Stack Overflow post leading me to believe it's an issue with typescript-cookie
.
Additional context
I'm using the library within a project created with create-react-app. In the above linked Stack Overflow post, the other user had the same error within a project created with "VITE + TS."
For testing allowed cookie name/value values property based testing should be useful: https://github.com/dubzzz/fast-check
With modules we can solve this with exports
, no need to construct an object any longer. Will also allow for better tree-shakability, when for instance only writing cookies (?)
export { get, set, remove }
and
import * as Cookies from 'ts-cookie'
How to solve withConverter()
, withAttributes()
though?
Describe the bug
A clear and concise description of what the bug is.
To Reproduce
Steps to reproduce the behavior:
Expected behavior
A clear and concise description of what you expected to happen.
Screenshots
If applicable, add screenshots to help explain your problem.
Desktop (please complete the following information):
Smartphone (please complete the following information):
Additional context
Add any other context about the problem here.
An error Module not found: Error: Package path . is not exported from package /Users/ishotihadus/test/node_modules/typescript-cookie (see exports field in /Users/ishotihadus/test/node_modules/typescript-cookie/package.json)
occurs when building with webpack in some cases.
I think this is caused by package.json:
"exports": {
".": {
"import": "./dist/typescript-cookie.mjs",
},
"./package.json": "./package.json"
},
I think there are two candidates to solve this problem.
"exports": {
".": {
"import": "./dist/typescript-cookie.mjs",
"require": "./dist/typescript-cookie.mjs",
},
"./package.json": "./package.json"
},
or
"exports": {
".": "./dist/typescript-cookie.mjs",
"./package.json": "./package.json"
},
If this restriction to import
is not necessary, I think the above solutions are helpful.
import * as webpack from 'webpack';
const config: (env: any) => webpack.Configuration = (env) => {
return {
mode: 'production',
entry: { 'test.js': './test.ts' },
module: {
rules: [
{
test: /\.(ts|tsx)$/i,
exclude: /(^|\/)node_modules\//,
use: [{ loader: 'ts-loader' }]
}
]
}
};
};
export default config;
{
"compilerOptions": {
"target": "ESNext",
"module": "commonjs",
"strict": true,
}
}
import { setCookie } from 'typescript-cookie';
setCookie('hoge', 'piyio', { expires: 60, secure: true, domain: 'example.com' });
Webpack can build without an error.
Describe the bug
If you use the new nodeResolution
bundler
option introduced in TypeScript 5.0 tsc will not find the declaration file for its imports and throw the following error:
error TS7016: Could not find a declaration file for module 'typescript-cookie'. 'D:/Test/.yarn/cache/typescript-cookie-npm-1.0.4-67c5369c2e-9fb4c76bd2.zip/node_modules/typescript-cookie/dist/typescript-cookie.mjs' implicitly has an 'any' type.
There are types at 'D:/Test/.yarn/cache/typescript-cookie-npm-1.0.4-67c5369c2e-9fb4c76bd2.zip/node_modules/typescript-cookie/dist/src/index.d.ts', but this result could not be resolved when respecting package.json "exports". The 'typescript-cookie' library may need to update its package.json or typings.
1 import { getCookie, removeCookie, setCookie } from 'typescript-cookie';
To Reproduce
Steps to reproduce the behavior:
import { setCookie } from 'typescript-cookie';
Expected behavior
I expected tsc to find the type declaration file for 'typescript-cookie'.
Desktop (please complete the following information):
Describe the bug
Cannot find module 'typescript-cookie' or its corresponding type declarations.
To Reproduce
1.- Install the latest release v1.0.0 via: npm i typescript-cookie
2.- Import the getCookie as the documentation says
3.- VSC display error about missing library or type declarations
Expected behavior
Expect no error on a TS library about missing types. Maybe I'm doing something wrong.
Desktop (please complete the following information):
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.