Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

What about types? #115

Closed
eatsjobs opened this issue Apr 14, 2024 · 27 comments
Closed

What about types? #115

eatsjobs opened this issue Apr 14, 2024 · 27 comments

Comments

@eatsjobs
Copy link
Contributor

as per title I saw there are not types nor @types/uhtml package. am I missing something?

@WebReflection
Copy link
Owner

you TS folks need to talk 😅
#112

@eatsjobs
Copy link
Contributor Author

@WebReflection I see. I opened an MR #116 would mind review it?
Types were buggy cause some types were(are) pointing to relative .js files and placing types in a specific folder was breaking the resolution.

@WebReflection
Copy link
Owner

@eatsjobs I really don't like all that noise in the repo ... I mean, locally in my editor, is there really nothing we can do to have all types correctly resolved in a folder a part? this should also create types only once because the cjs artifact is a 1:1 representation of the esm one so one types folder from esm should satisfy both cases.

@eatsjobs
Copy link
Contributor Author

Hey @WebReflection unfortunately not that I'm aware of. The problem are the types with dynamic import like
typeof import("./relative.js").MyObject. If we move the types in another folder this will not work unless you rewrite the relative dynamic import somehow. you're using rollup have you tried this already? https://www.npmjs.com/package/rollup-plugin-dts

@WebReflection
Copy link
Owner

@eatsjobs

This project is in maintenance mode.

nope, and not planning to ... I wish there was https://www.npmjs.com/package/rollup-plugin-jsdoc for JSDoc TS that would do whatever the dts plugin does at the end ... we can't have it all, it seams ... btw, with latest changes with exports, do you still have issues with types? talking about 4.5.1

@eatsjobs
Copy link
Contributor Author

https://www.npmjs.com/package/uhtml?activeTab=code cannot see the .d.ts file in the npm package. am I missed something? I don't see a "file" field in the package.json 🤔

@eatsjobs
Copy link
Contributor Author

eatsjobs commented Apr 15, 2024

Since my changes are not in main even if you merge it maybe you rebased? anyway I re-prepare the "same" MR #119
@WebReflection by the way yes solves my typings issue

@WebReflection
Copy link
Owner

@eatsjobs I've reverted your change because it breaks on postbuild ... I think if I try it again it will break again and that's a no-go to me as I run build before publishing and the errors block my flow.

@eatsjobs
Copy link
Contributor Author

@WebReflection Ok I can work on that

@eatsjobs
Copy link
Contributor Author

@WebReflection Once you release 4.5.2 we can easily see if the types are working here https://stackblitz.com/edit/vitejs-vite-udoa4c?file=src%2Fcounter.ts

@eatsjobs
Copy link
Contributor Author

I'd like to leave also this article here cause I think is related. https://blog.isquaredsoftware.com/2023/08/esm-modernization-lessons/ what a time to be a package maintainer!

@iacore
Copy link

iacore commented Apr 18, 2024

as per title I saw there are not types nor @types/uhtml package. am I missing something?

Types should have worked already without the .d.ts files. Did it not work for you?

update: I see you mention stackblitz. I use the built-in Typescript LSP from VS Code.

@iacore
Copy link

iacore commented Apr 18, 2024

Types are broken again on v4.5.5. uhtml/ssr has the wrong type file.

v4.5.2 is fine. Both Deno and tsc on Node.JS accept that.

To test this yourself, save the following two files as a.ts and b.ts, and run both with deno check a.ts or deno check b.ts.

This works. If you change initSsr() to initSsr.a(), it will show a type error, which is expected.

import initSsr from 'https://esm.sh/uhtml@4.5.2/ssr';

const { html, render } = initSsr()

const abc = [1,2,3]

console.log(html`hi, i can count to three! ${abc.map((x,i) => html`${x}${i==2?'!':', '}`)}`.toDOM().toString())

This already shows a type error without modification. ("initSsr is not callable")

import initSsr from 'https://esm.sh/uhtml@4.5.5/ssr';

const { html, render } = initSsr()

const abc = [1,2,3]

console.log(html`hi, i can count to three! ${abc.map((x,i) => html`${x}${i==2?'!':', '}`)}`.toDOM().toString())

@eatsjobs
Copy link
Contributor Author

as per title I saw there are not types nor @types/uhtml package. am I missing something?

Types should have worked already without the .d.ts files. Did it not work for you?

update: I see you mention stackblitz. I use the built-in Typescript LSP from VS Code.

no they don't work with latest version of typescript and a vanilla-ts vite template like you can test it in stackblitz link

@eatsjobs
Copy link
Contributor Author

eatsjobs commented Apr 18, 2024

btw this works with deno

import initSsr from "https://cdn.jsdelivr.net/npm/uhtml@4.5.5/ssr/+esm";

const { html, render } = initSsr();

const abc = [1, 2, 3];

console.log(
  html`hi, i can count to three!
  ${abc.map((x, i) => html`${x}${i == 2 ? "!" : ", "}`)}`
    .toDOM()
    .toString()
);

I think is more something related on how esm.sh handle the package. is there a way with deno to install an npm package? I read about jsr. maybe can help you. but jsdeliver works just fine so give it a try

@eatsjobs
Copy link
Contributor Author

@iacore if you want to test all possible types resolution you can do it with https://www.npmjs.com/package/@arethetypeswrong/cli which I added in last MR

Support both cjs and esm is possible but tricky and honestly don't worth the effort.
Here is the table of types resolution for 4.5.5: https://arethetypeswrong.github.io/?p=uhtml%404.5.5
So only esm types. esm.run is probably doing a "wrong" types resolution and was doing some magic behind the scenes with jsdoc.

@eatsjobs
Copy link
Contributor Author

@iacore
Copy link

iacore commented Apr 18, 2024

So only esm types. esm.run is probably doing a "wrong" types resolution and was doing some magic behind the scenes with jsdoc.

Typescript and node module resolution is cursed.

I think is more something related on how esm.sh handle the package. is there a way with deno to install an npm package?

Yes. It's also broken, but in another way.

❯ cat a.ts
import initSsr from 'npm:uhtml@4.5.2/ssr';

const { html, render } = initSsr()

const abc = [1,2,3]

console.log(html`hi, i can count to three! ${abc.map((x,i) => html`${x}${i==2?'!':', '}`)}`.toDOM().toString())

❯ deno check a.ts
Check file:///tmp/a.ts
error: Uncaught Error: Failed resolving package subpath './ssr' for '/tmp/node_modules/.deno/uhtml@4.5.2/node_modules/uhtml/package.json': [ERR_INVALID_PACKAGE_TARGET] Invalid "exports" target {"import":"./esm/init-ssr.js","default":"./cjs/init-ssr.js"} defined for './ssr' in the package config /tmp/node_modules/.deno/uhtml@4.5.2/node_modules/uhtml/package.json imported from file:///tmp/a.ts; target must start with "./"
    at Object.resolveModuleNames (ext:deno_tsc/99_main_compiler.js:697:28)
    at actualResolveModuleNamesWorker (ext:deno_tsc/00_typescript.js:120758:144)
    at resolveModuleNamesWorker (ext:deno_tsc/00_typescript.js:121155:22)
    at resolveModuleNamesReusingOldState (ext:deno_tsc/00_typescript.js:121250:16)
    at processImportedModules (ext:deno_tsc/00_typescript.js:122813:120)
    at findSourceFileWorker (ext:deno_tsc/00_typescript.js:122544:9)
    at findSourceFile (ext:deno_tsc/00_typescript.js:122408:22)
    at ext:deno_tsc/00_typescript.js:122357:24
    at getSourceFileFromReferenceWorker (ext:deno_tsc/00_typescript.js:122326:28)
    at processSourceFile (ext:deno_tsc/00_typescript.js:122355:7)

@eatsjobs
Copy link
Contributor Author

@iacore #120 if you could test it in local with deno would be really great

@iacore
Copy link

iacore commented Apr 18, 2024

Thanks for telling me JSR. I'll take a look if I can publish uhtml there. @WebReflection can I do that?

@WebReflection
Copy link
Owner

I want this infinite types story done then I will publish on JSR as I have alreday my @wr namespace there

@WebReflection
Copy link
Owner

I went ahead and published this #121

Can anyone please confirm 4.5.6 now works as expected? Thank you!

@iacore
Copy link

iacore commented Apr 19, 2024

Type resolution is correct on Deno. Type definition is somewhat incorrect.

❯ head -n1 a.ts
import initSsr from 'npm:uhtml@4.5.6/ssr';

❯ deno check a.ts
Check file:///tmp/abc/a.ts
error: TS2555 [ERROR]: Expected at least 1 arguments, but got 0.
const { html, render } = initSsr()
                         ~~~~~~~
    at file:///tmp/abc/a.ts:3:26

    An argument for 'content' was not provided.
    declare function initSsr(content: any, ...rest: any[]): typeof import("./keyed.js");
                             ~~~~~~~~~~~~
        at file:///tmp/abc/node_modules/.deno/uhtml@4.5.6/node_modules/uhtml/types/init-ssr.d.mts:9:26

@WebReflection
Copy link
Owner

@iacore how about now with 4.5.7 ??? I've fixed that signature which was indeed wrong.

@iacore
Copy link

iacore commented Apr 19, 2024

string | null is not optional (undefined).

See #122

@WebReflection
Copy link
Owner

4.5.8 it is then

@eatsjobs
Copy link
Contributor Author

If everything is working also for @iacore you can close the issue for me @WebReflection Thanks!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants