vadimdemedes/ink

TypeScript doesn't find type declarations in v4.0.0

AlCalzone opened this issue · 10 comments

I believe package.json must be changed in one of the following ways:

	"type": "module",
-	"exports": {
-		"types": "./build/index.d.ts",
-		"default": "./build/index.js"
-	},
+	"module": "./build/index.js",
+	"types": "./build/index.d.ts",

(tested, works) or

	"type": "module",
	"exports": {
-		"types": "./build/index.d.ts",
-		"default": "./build/index.js"
+		".": {
+			"types": "./build/index.d.ts",
+			"default": "./build/index.js"
+		}
	},

according to https://www.typescriptlang.org/docs/handbook/release-notes/typescript-4-7.html#packagejson-exports-imports-and-self-referencing

No, the current config is correct. The issue is most likely with your build tool.

Ok, so I did this

npm init -y
npm i typescript ink-repro
echo 'import {render} from "ink"' > index.mts
code index.mts

and got:
grafik

It does start working after I add a tsconfig with the corresponding module and moduleResolution flags (or change it in an existing project).
I'd argue though that adding two lines (first diff) is a small price to pay to allow consumers not to change their config, causing other issues down the line.

I have the same issue.

@sindresorhus Not sure what "issue with your build tool" means if all other packages work just fine.

I'm new to Ink, is it ESM only? (similar to how the latest versions of Chalk are). @AlCalzone seems to be using "mts" files. Changing my module would be more of a hassle and if Ink is now ESM only, I don't quite understand packages like Chalk that force ESM to their consumers 😅 But I'm not here to debate build practices, I'd just like a solution that doesn't break the rest of my development workflow.

Furthermore, on NPM, there seem to be no included type definitions, hence the "DT" logo, suggesting a @types package, but the package has a deprecation notice stating it's a stub. However, I can confirm that they are present in my node_modules, but I don't understand why TypeScript (and NPM) isn't seeing the declarations. Again, Ink is the only package in my project with this kind of issue.

EDIT:
Installing v3.2.0 works with no issue regarding the type definition. NPM also seems to recognize the included types in that version, hence the "TS" logo.

I'm new to Ink, is it ESM only?

Yes:

"type": "module",

@maxijonson You don't actually have to use mts files, but typescript's module and moduleResolution fields must be set to node16.

Alternatively you could use yarn patch to edit the package.json of ink like my first diff, which also works and is a less invasive measure than the tsconfig change.

I understand why so many packages are being converted to ESM, I just don't understand why it has to be in the most breaking way possible 🤷🏻‍♂️

Thanks, I followed what you said and my project is now ESM 😅

I only had an issue with useStdoutDimensions not being able to be used because "require() of Ink from useStdoutDimensions is not supported" and I don't feel like I know enough about ESM errors to open an issue, in case it's just my project. Hopefully, I won't have any more compatibility issues!

Same. I just copied that hook into my project and compiled it myself.