-
Notifications
You must be signed in to change notification settings - Fork 0
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
Add .d.ts
files for improved TypeScript support
#33
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
|
@baseplate-admin can you test this out for me please? Best method is going to be for you to clone locally and link it in your project. Here some quick guidance though
Once done testing, reverse the link to your local copy via |
p.s. Since I didn’t mention it explicitly (sorry I forgot): Be sure to run |
Hi so i found a bug outside of this PR. Line 53 in 6c6e2ba
This should be import type { SvelteComponent } from "svelte";
* @param {typeof SvelteComponent<{}>} opts.component Svelte component instance to incorporate into your custom element. |
Shouldn't that be
I could incorporate into this PR. If you want. Just ensure you are checked out to |
Alternatively (if easier) -- fork |
Actually i did check the PR, i used this in all my projects. on another note i am looking into the repository |
@baseplate-admin do me a favor please and give me a version of this branch that you see is ideal so I can try it out. This back/forth would probably be a bit more efficient if maybe you showed me a version I can check out (or merge). You may include a version that commits |
p.s. @baseplate-admin in my main TypeScript project, this is what I did. However, I am not 100% sure what would need to be done in a JS-only project like this one when using Just to give you an idea, here what I ended up doing (probably pretty amateur) put into // Generic type for anything that can be instantiated. Copied and modified from https://github.com/microsoft/TypeScript/issues/17572#issue-247539885
export type Instantiable = {new(...args: any[]): any};
// Setup to type hint Svelte component constructors (i.e. the default export of *.svelte files).
export type SvelteComponent = Instantiable; Then where I use import svelteRetag from 'svelte-retag';
import type { SvelteComponent } from './types';
/**
* Defines a custom element for the given Svelte component and hyphenated tag name.
*
* @param {SvelteComponent} component The svelte component constructor. Basically, the default export from your MyComponent.svelte file.
*
* @param {string} tagName The name of the tag. Must contain a dash and be all lower case.
* e.g. 'my-component' (for <my-component> tag).
*
* @param {array} syncProps For reactively synchronizing changes to the element attributes with the props on
* the Svelte component. Not necessary for initialization, just for changes
* performed after mount.
*/
export function defineTag(component: SvelteComponent, tagName: string, syncProps = []) {
svelteRetag({
component: component,
tagname: tagName,
attributes: syncProps,
});
} |
Fair enough, give me some time to get personal life sorted then i will send a PR |
411e5d0
to
d8ec11f
Compare
Hi, Sorry for late reply. This works fine for my project :) Lemme fix the remaining issues. |
Ok @baseplate-admin I think I know what you meant -- was it that when you added that Essentially:
Via JSDoc: /**
* @typedef {new(...args: any[]) => any} Newable
* @typedef {Newable} SvelteComponent
*/ And that effectively generates this /**
* @typedef {new(...args: any[]) => any} Newable Type alias for a really generic class constructor
* @typedef {Newable} SvelteComponent Svelte component class constructor (basically a "newable" object)
*/
/**
* Please see README.md for usage information.
*
* @param {object} opts Custom element options
*
* @param {SvelteComponent} opts.component The Svelte component *class* constructor to incorporate into your custom element (this is the imported component class, *not* an instance)
* @param {string} opts.tagname Name of the custom element tag you'd like to define.
* @param {string[]} [opts.attributes=[]] Optional array of attributes that should be reactively forwarded to the component when modified.
* @param {boolean} [opts.shadow=false] Indicates if we should build the component in the shadow root instead of in the regular ("light") DOM.
* @param {string} [opts.href=""] URL to the CSS stylesheet to incorporate into the shadow DOM (if enabled).
*
* Experimental:
* @param {boolean} [opts.hydratable=false] Light DOM slot hydration (specific to svelte-retag): Enables pre-rendering of the
* web component (e.g. SSR) by adding extra markers (attributes & wrappers) during
* rendering to enable svelte-retag to find and restore light DOM slots when
* restoring interactivity.
*
* @param {boolean|string} [opts.debugMode=false] Hidden option to enable debugging for package development purposes.
*/
export default function svelteRetag(opts: {
component: SvelteComponent;
tagname: string;
attributes?: string[];
shadow?: boolean;
href?: string;
hydratable?: boolean;
debugMode?: boolean | string;
}): void;
/**
* Type alias for a really generic class constructor
*/
export type Newable = new (...args: any[]) => any;
/**
* Svelte component class constructor (basically a "newable" object)
*/
export type SvelteComponent = Newable; |
Fair warning @baseplate-admin
Good luck. |
…nt a "newable" constructor type and also fixing the optional types by defining their defaults, so the generated .d.ts properly suffixes the "?" at the end of each optional field.
3645fd0
to
1efa138
Compare
You can also test this as a beta release, too (what I'm doing in my project, since symlinks aren't great for Intellisense in my case on Windows): npm install [email protected] Edit: Then in your import type { SvelteComponent } from 'svelte-retag/types'; 🤔 Wonder if I should give that another name. |
import svelteRetag from "svelte-retag";
const mappings = [
{ tagname: "markdown", component: await import("$components/minor/Markdown/Index.svelte"), attributes: [`markdown`, `class`] },
{ tagname: "scroll-area", component: await import("$components/minor/ScrollArea/Index.svelte"), attributes: [`parent_class`, `offset_scrollbar`, `gradient_mask`, `class`] },
{ tagname: "comment", component: await import("$components/minor/Comment/Index.svelte"), attributes: [`api_url`] }
];
mappings.forEach((item) => {
svelteRetag({
component: item.component.default,
tagname: `coreproject-${item.tagname}`,
attributes: item.attributes, // Changes to these attributes will be reactively forwarded to your component
shadow: false, // Use the light DOM
hydratable: false
});
}); This type of code works with
This is not necessary
My god this is a godsend, i cant thank you how much this helps. Thank you for this :D |
Sweet. I’ll work on this tomorrow and probably have 1.5.0 out by then. Also @baseplate-admin
|
p.s.
You’re very welcome! To be fair, I’m obviously a big user of |
… custom type for 'component' to prevent any confusion with an actual Svelte type.
This is published now as |
Ah, never mind... false alarm. It was a caching issue on my end. |
Implement #30.