-
Notifications
You must be signed in to change notification settings - Fork 1k
Make our directives generic, for better type checking of lit templates #5035
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
Conversation
🦋 Changeset detectedLatest commit: 3af7b31 The changes in this PR will be included in the next version bump. This PR includes changesets to release 1 package
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
📊 Tachometer Benchmark ResultsSummarynop-update
render
update
update-reflect
Resultsthis-change
render
update
update-reflect
this-change, tip-of-tree, previous-release
render
update
nop-update
this-change, tip-of-tree, previous-release
render
update
this-change, tip-of-tree, previous-release
render
update
update-reflect
|
4f09a67 to
e01f2d3
Compare
|
The size of lit-html.js and lit-core.min.js are as expected. |
justinfagnani
left a comment
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.
LGTM with one question.
I think you need to run format too.
packages/lit-html/src/directive.ts
Outdated
|
|
||
| export interface DirectiveClass { | ||
| new (part: PartInfo): Directive; | ||
| export interface DirectiveClass<RenderAs = unknown> { |
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.
Why do you need this type parameter? You'd normally get the return type with ReturnType<C['render']>.
Is it so that you can feed in a type variable from an override type for directive(), like in Guard? If so, can you leave a comment?
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.
We can actually use infer (and similar tricks in the TypeScript type system) to get around needing this at all. I've updated the PR to not need any changes to the common directive types, with a little care, a directive can just be written to put enough information on the DirectiveResults it makes.
| } | ||
|
|
||
| interface Until { | ||
| <T>(val: T): DirectiveResult<typeof UntilDirective<T>>; |
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.
Why can't this be:
| <T>(val: T): DirectiveResult<typeof UntilDirective<T>>; | |
| <T>(...args: Array<T>): DirectiveResult<typeof UntilDirective<T>>; |
Won't TS infer T as a union of the arg types, just like you're doing manually?
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.
Paired on fix.
…plates. This allows a custom directive to carry enough information that the TypeScript type system can tell the type that a DirectiveResult will render. This is backwards compatible, existing directives will generally be inferred to render the type of their `render` functions. Many directives can be updated to themselves be generic, to better express their types, and I've done this for our built in directives. By itself, this doesn't do much, but I've also got a patch to rune's lit analyzer to better type check directives there.
Turns out, all we need is to make our own generic directives generic enough so that we can infer the return type of their render functions from their DirectiveResult. No need to change directive.ts or async-directive.ts at all.
2180310 to
8cee2db
Compare
If a directive's type is written such that it returns sufficiently specific DirectiveResults, the TypeScript type system can tell the effective type that a DirectiveResult will render to its part.
By itself, this doesn't do much, but I've also got a patch to rune's lit analyzer to better type check directives there.