You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When using iti in a TypeScript project with "moduleResolution": "NodeNext" in your tsconfig.json, type information for iti cannot be located.
This line:
import { createContainer } from 'iti'
Produces the error:
Could not find a declaration file for module 'iti'. '<project path>/node_modules/iti/dist/iti.modern.js'
implicitly has an 'any' type.
Try `npm i --save-dev @types/iti` if it exists or add a new declaration (.d.ts) file
containing `declare module 'iti';`
The project will still compile and run with tsc; you just don't have type information for iti for static checking, linting, etc.
Cause
There are two causes:
The exports map in iti's package.json contains an exports map, but the types key is not defined for each export. See this TypeScript issue for a complete explanation. When exports is defined, top-level types or typings keys are ignored.
The export ... from statements in iti/src/index.ts need to use an explicit .js extension. Without this, import { createContainer } from 'iti' will nominally succeed, but the type of createContainer will just be import, and the static analysis gets very confused.
Proposed fix
The first issue is simple: just add "types": "./dist/src/index.d.ts" to the exports map in package.json.
For the second issue, I'll be honest: I'm fairly new to JS/TS. My understanding is that explicit .js extensions are required for import/export statements when using ESM ("type": "module" in package.json). So I'm not sure how iti is getting away with "bare" imports in the first place. But in any case, adding extensions only to the export statements in src/index.ts is enough to satisfy the consumer's linter and should be backwards-compatible with CJS. With that said, two of the iti tests (dispose....) try to import { createContainer } from '../src', which needs to be ../src/iti after this change. The other tests already import this way.
I have a fork with these proposed changes made to iti and iti-react. I can make a PR if you'd like.
The text was updated successfully, but these errors were encountered:
Problem Description
When using iti in a TypeScript project with
"moduleResolution": "NodeNext"
in yourtsconfig.json
, type information for iti cannot be located.This line:
import { createContainer } from 'iti'
Produces the error:
The project will still compile and run with
tsc
; you just don't have type information for iti for static checking, linting, etc.Cause
There are two causes:
The
exports
map in iti'spackage.json
contains anexports
map, but thetypes
key is not defined for each export. See this TypeScript issue for a complete explanation. Whenexports
is defined, top-leveltypes
ortypings
keys are ignored.The
export ... from
statements initi/src/index.ts
need to use an explicit.js
extension. Without this,import { createContainer } from 'iti'
will nominally succeed, but the type ofcreateContainer
will just beimport
, and the static analysis gets very confused.Proposed fix
The first issue is simple: just add
"types": "./dist/src/index.d.ts"
to theexports
map inpackage.json
.For the second issue, I'll be honest: I'm fairly new to JS/TS. My understanding is that explicit
.js
extensions are required for import/export statements when using ESM ("type": "module"
inpackage.json
). So I'm not sure how iti is getting away with "bare" imports in the first place. But in any case, adding extensions only to theexport
statements insrc/index.ts
is enough to satisfy the consumer's linter and should be backwards-compatible with CJS. With that said, two of the iti tests (dispose....
) try toimport { createContainer } from '../src'
, which needs to be../src/iti
after this change. The other tests already import this way.I have a fork with these proposed changes made to
iti
anditi-react
. I can make a PR if you'd like.The text was updated successfully, but these errors were encountered: