-
-
Notifications
You must be signed in to change notification settings - Fork 112
Add typings #201
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 typings #201
Conversation
Hey @rikuba this is pretty AWESOME, thanks !!! You just reminded me I had in mind to try with some nonsense like the following one (I really have no idea how to describe types in TypeScript, sorry) declare class Component {
html?: IWiredTag;
svg?: IWiredTag;
render(...args:any[]): IWiredTag;
state: object;
get defaultState(): object;
setState(state:object, doNotRender?:boolean): Component;
handleEvent(event:Event): void;
}
declare class Wire {
constructor(childNodes: [HTMLElement|Text, ...HTMLElement[] | ...Text[], HTMLElement|Text]);
insert(): DocumentFragment;
remove(): HTMLElement|Text;
}
declare function hyperHTML(node: HTMLElement | SVGElement): IBoundTag;
declare function hyperHTML(identity?: object): IWiredTag;
declare function hyperHTML(identity: object, type?:'html|svg'): IWiredTag;
declare function hyperHTML(identity: object, id?:':any-id'): IWiredTag;
declare function hyperHTML(identity: object, type_id?:'html:any-id'): IWiredTag;
interface hyperHTML {
bind(node: HTMLElement | SVGElement): IBoundTag;
define(intent: string, callback: Function):void;
wire(identity?: object): IWiredTag;
wire(identity: object, type?:'html|svg'): IWiredTag;
wire(identity: object, id?:':any-id'): IWiredTag;
wire(identity: object, type_id?:'html:any-id'): IWiredTag;
hyper(node: HTMLElement | SVGElement): IBoundTag;
hyper(identity?: object): IWiredTag;
hyper(identity: object, type?:'html|svg'): IWiredTag;
hyper(identity: object, id?:':any-id'): IWiredTag;
hyper(identity: object, type_id?:'html:any-id'): IWiredTag;
}
export interface IBoundTag {
(template:string[], ...interpolations:any[]): HTMLElement | SVGElement | Text | Wire;
}
export interface IWiredTag {
(template:string[], ...interpolations:any[]): HTMLElement | SVGElement | Text | Wire;
} You have definitively a better way to describe this library but I wonder if:
Thanks a lot for this collaboration, please ask me anything you want !!! |
Thank you for your reply.
Please edit and commit freely. |
Hi @rikuba , thanks again. Funny TypeScript has no way to define getters on the class level!!! Anyway, the wired function returns Element most of the time but if there are multiple nodes at the same level It returns a specialised Wire instance. You can pass the regular return as appendChild argument, you need to use Maybe I should offer a Thoughts ? |
I felt that to offer type TemplateFunction<T> = (template: TemplateStringsArray, ...values: any[]) => T;
type WiredTemplateFunction = TemplateFunction<ValuableElement | Wire>;
declare class ValuableElement extends Element {
valueOf(): Element;
}
declare class Wire {
valueOf(): DocumentFragment;
// other properties should not be public?
}
export declare function wire(identity?: object | null, type?: 'html' | 'svg'): WiredTemplateFunction;
export declare function wire(identity?: object | null, type_id?: string): WiredTemplateFunction;
//
const elementOrWire = wire({})`<i></i><i></i>`; // this type is (ValuableElement | Wire)
const node = elementOrWire.valueOf(); // this type is (Element | DocumentFragment)
document.body.appendChild(node); // ok |
Hi @rikuba, I think for now we can keep I like the seamless experience through Example, like you said one might expect to append a Wire like that, but trying to remove it will throw errors because it's a fragment, not the list of nodes. I rather have These two are indeed the only methods in the Wire, and are used internally already. declare class Wire {
constructor(childNodes:Element[]);
insert(): DocumentFragment;
remove(): Element;
} The Inserting 2 or more elements requires a fragment, which is returned by TL;DR const wired = wire()`<i/><i/>`; // 2 adjacent <i> elements
document.body.appendChild(wired.insert());
document.body.removeChild(wired.remove()); One last little thing If specified as The method will also return Can you please put these info in too? Thank you! edit just to clarify even more setState(state: Partial<T> | ((this: this, state: T) => Partial<T>), render?: true): Component; not sure when you return |
Please don't hate me, one more thing:
It's last part of the Component refactoring I'd love to type/describe too. Then I'll merge, I promise 😃 |
hyper: typeof hyper; | ||
wire: typeof wire; | ||
|
||
// hyper(null, 'html')`HTML` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello,
first, thank you for documenting all this :) 👍 That will be very helpful!
I have a question about how to document things in TS. Guess the comments with simple double slash //
are considered simple comments and comments with / ** text **/
right before a declaration are considered useful comments.
Example, if I change this line to something like this:
/** hyper(null, 'html')`HTML` **/
(identity: null | undefined, type?: 'html' | 'svg'): WiredTemplateFunction;
And that could be the first step on documenting each class, fields and fn. 😉
Cheers 🍺
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that's pretty cool but I think you don't need double stars (indeed there is an extra star showing off after the comment) and apparently markdown is enabled so that backtick transforms `HTML`
into code HTML
.
I'd say for now let's just ship types, then I'll try to describe as best as I can the rest.
I don't want to bother anyone too much about this already awesome PR, cheers!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When this is merged, I'd be happy to contribute to the docs as well ;)
this went live into Thank you for the patience and the effort, next up: document all of it properly !!! |
ref #187