-
-
Notifications
You must be signed in to change notification settings - Fork 501
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
Configuration API (UserConfig) types, TypeScript mega issue for 3.0 #3097
Comments
This doesn't work for me: import { UserConfig } from "@11ty/eleventy"; I need to do: import UserConfig from "@11ty/eleventy/src/UserConfig.js";
|
see also #3127 |
#3060 removed the .d.ts file to rely on JSDoc entirely, but the .d.ts had been exporting UserConfig (https://github.com/11ty/eleventy/blob/ecd0579a2dded6939510b7c23841388b26eb6d16/src/index.d.ts) and the issue now that is UserConfig isn't exported anywhere else. Would the fix be to export it here? |
Agree. I have |
You don't have to do that, can't you just use it like |
Unfortunately, no... Today I migrated to a new version. I'm so happy that the number of cjs files is decreasing in my projects 🎉 After migration, I encountered two issues.
As I mentioned earlier, my project is configured in a way that TypeScript performs checks on all When TypeScript encounters an import of a package that has no types, it suggests two solutions:
I think before migrating to ESM, this was a rare problem, as I mentioned, many linters ignored |
While migrating to 11tyv3.0.0-alpha.5, the JSDoc comments stopped working for me. It seems that in ESM, you have to explicitly specify the default export. /** @param { import("@11ty/eleventy").UserConfig } eleventyConfig */ ❌
/** @param { (import("@11ty/eleventy/src/UserConfig").default) } eleventyConfig */ ✅ this works fine as well: /**
* @typedef {import('@11ty/eleventy/src/UserConfig').default} EleventyConfig
* @typedef {import('@11ty/eleventy/src/defaultConfig').defaultConfig} EleventyReturnValue
* @type {(eleventyConfig: EleventyConfig) => EleventyReturnValue}
*/ |
See #3291 |
Closing this but would appreciate further confirmation! |
I have
|
@tbroyer (and @what1s1ove), I just tested /** @param {import("@11ty/eleventy").UserConfig} eleventyConfig */
export default function (eleventyConfig) { Here's a minimal stackblitz link to confirm: https://stackblitz.com/edit/github-7xpr1h-tbkpjx?file=.eleventy.js Maybe you can share a minimal repro if it's not working for you? If you have a custom |
Got it: this is Because 3.0.0-alpha.14 had a Should a |
Spent some time looking into this and found a few things worth noting: v3.0.0-alpha.14 failed
In v3.0.0-alpha.14 Irrespective of The The previous approach worked better only because it exported import UserConfig from "./src/UserConfig.js";
export { UserConfig }; That said, I think it’d be useful to have IDE autocomplete and I’ve filed the Eleventy.js work at #3383. |
Alright I have a bit more clarity on this now. The previous
The good part of this exploration is that I’m well educated on the topic now and I’m not opposed to including We’ll keep the existing So, TL;DR I do plan to merge #3384 in service of the long term goal of shipping We’ll continue to recommend folks use https://www.11ty.dev/docs/config/#type-definitions for IDE autocomplete. If I’ve missed something important here, please let me know! |
Would you ever consider writing Eleventy in TS itself? And then compiling down to JS with generated type definition files? |
@uncenter No, probably not. TypeScript can export |
Just some clarification on the history here, since it’s been a bit all over the place and it’s worth documenting at the very least for future me (sorry folks, I’m more educated on TypeScript now 😭).
|
You can (technically and unofficially) use TypeScript for the main configuration (e.g.
Essentially, the Unfortunately, since the emitted Use |
*sneaks #582 as the original PR in |
What could be an option: Following node's lead and have a package in DefinitelyTyped. For example, I spotted DefinitelyTyped/DefinitelyTyped#69010 Documentation: https://github.com/DefinitelyTyped/DefinitelyTyped#how-can-i-contribute |
Having a DefinitelyTyped package would be a good holdover, but in what I've learned so far about TS it's more terse if .d.ts files can be installed along with the package. I'd say if it's possible for types to be maintained centrally without having to rely on another package, use a community solution like I also saw another option get mentioned over on #3296: expose a |
FWIW 11ty.ts is probably the most complete and well typed solution available (currently) in this eco. The There is also the matter of plugins which a lot of folks seem to forget. 11ty.ts will auto-type plugins that expose declarations using a typical syntactical structure of plugin parameters by cherry-picking index 1 of the argument order. |
@panoply does 11ty.ts support 11ty v3? |
I have only just upgraded locally. Even though it's lacking some of the v3 API features, it comes with vast majority of the API already cooked so v3 API is by default already largely supported minus some of the new methods available and deprecation notices. If I get some time tomorrow I'll update and bring support. |
@briancarbone I never thanked you for this suggestion. I searched for weeks for a solution to get TypeScript to pick up Eleventy's JSDoc comments so I could use them in my plugin and this suggestion was what did it! It might be possible to import the types directly without having to set up Typescript like this, EDIT: Yes, subpath exports will allow Typescript to import a module directly, if declared. |
The UserConfig not exported in
get this error while using them, the Lines 11 to 14 in 8675d68
in the Lines 11 to 19 in cc7e5a5
the types field in the package.json must be |
tsc can generate types from jsdoc, but require to config them to generate This is part of documention:
The last two steps have not been completed. {
"declaration": true,
"emitDeclarationOnly": true,
}
// https://www.typescriptlang.org/docs/handbook/declaration-files/dts-from-js.html#tsconfig I'm do this in the local and all types is generated and work fine, need apply this to 11ty? |
I read the various sections of the documentation and some issues/prrs |
Strange! My problem is that I can't get it to work without a hint: I'm use typescript for config file |
Fwiw @njfamirm looks like your website repository is on Eleventy v2? This issue is specifically for v3 types. |
No, I'm use v3 |
@Ryuno-Ki I have it working but it also stopped working for some reason 🤷 @njfamirm I managed to get it working via JSDoc like this This is for /** @param {import("markdown-it").default} mdlib */
function amedMd(mdlib){
mdlib.use() // makrdown-it completion here
}
/**
* @param {import("@11ty/eleventy/UserConfig")} eleventyConfig
*/
export default function(eleventyConfig){
eleventyConfig.amendLibrary("md",(mdlib)=>amedMd(mdlib) )
} this is for
I have been hesitating to switch to TS but learning APIs in JS is nothing but a waste of time, I really with JS had type annotations instead of a forever fork of TypeScript. |
I'll sort out v3 on 11ty.ts. Majority of the API is covered. DefineConfig solves all the nuances: |
FWIW relying on JSDoc types through comment annotations will be flaky. It requires more work from the TS Lang Server. I explored this at great lengths. While comment annotations suffice for a lot, complex typings need more context, this is best achieved through either declarations or defineConfig. |
(This one is just a reminder for me)
See: https://www.11ty.dev/docs/config/#type-definitions
Pre-alpha I can confirm that v9 (ESM) of
eleventy-base-blog
with npm installed viafile:../eleventy
works fine but I want to remind myself to check post-publish too.The text was updated successfully, but these errors were encountered: