-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
**feat:** improve the
ElementAttributes
and `ReactElementAttributes…
…` JSX type helpers. This improves JSX types for Custom Elements in Solid JSX, React JSX, and Preact JSX, especially in React/Preact JSX whereas previously the React/Preact JSX prop types only accepted string values for dash-cased attributes. If you have a `get`ter/`set`ter property in your element class, you can now define a dummy property prefixed with `__set__<name-of-the-setter>` to specify the type of the `set`ter, and this will be picked up and lead to improved types in JSX. For example, you can start using like so: Before: ```js @element('some-element') class SomeElement extends Element { @Attribute get someProp(): number {...} @Attribute set someProp(n: number | 'foo' | 'bar') {...} } declare module 'react' { namespace JSX { interface IntrinsicElements { 'some-element': ReactElementAttributes<SomeElement, 'someProp'> } } } ``` and this JSX would have a type error: ```jsx return <some-element some-prop={'foo'} /> // Error: string is not assignable to number ``` After: ```js @element('some-element') class SomeElement extends Element { @Attribute get someProp(): number {...} @Attribute set someProp(n: this['__set__someProp']) {...} /** don't use this property, it is for JSX types. */ __set__someProp!: number | 'foo' | 'bar' } // ... the same React JSX definition as before ... ``` and now JSX prop types will allow setting the *setter* types: ```jsx return <some-element someProp={'foo'} /> // No error, yay! ``` Note, the property is camelCase instead of dash-case now. **BREAKING:** This may introduce type errors into existing JSX templates, tested with React 19 (not tested with React 18 or below), but it is an inevitable upgrade for the better. To migrate, there's likely nothing to do in Solid JSX, but in React JSX the selected properties are no longer converted to dash-case, so you'll want to use the original JS property names in React JSX templates. For example this, ```jsx return <some-element some-prop={...} /> ``` becomes ```jsx return <some-element someProp={...} /> ``` If you have any issues, please reach out on GitHub or Discord! https://discord.gg/VmvkFcWrsx Co-authored-by: bigmistqke <[email protected]>
- Loading branch information
1 parent
1811a92
commit babd555
Showing
18 changed files
with
356 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import type { ReactElementAttributes } from './react.js'; | ||
declare class SomeElement extends HTMLElement { | ||
someProp: 'true' | 'false' | boolean; | ||
get otherProp(): number; | ||
set otherProp(_: this['__set__otherProp']); | ||
/** do not use this property, its only for JSX types */ | ||
__set__otherProp: number | 'foo'; | ||
} | ||
declare module 'react' { | ||
namespace JSX { | ||
interface IntrinsicElements { | ||
'some-element': ReactElementAttributes<SomeElement, 'someProp' | 'otherProp'>; | ||
} | ||
} | ||
} | ||
export {}; | ||
//# sourceMappingURL=jsx-types-react.test.d.ts.map |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* @jsxImportSource react */ | ||
class SomeElement extends HTMLElement { | ||
someProp = true; | ||
get otherProp() { | ||
return 0; | ||
} | ||
set otherProp(_) { } | ||
/** do not use this property, its only for JSX types */ | ||
__set__otherProp; | ||
} | ||
SomeElement; | ||
describe('JSX types with ReactElementAttributes', () => { | ||
it('derives JSX types from classes', () => { | ||
; | ||
<> | ||
<some-element someProp="false" otherProp="foo"/> | ||
<some-element someProp="false" otherProp="foo"/> | ||
<some-element someProp={false} otherProp={123}/> | ||
{/* @ts-expect-error good, number is invalid */} | ||
<some-element someProp={123}/> | ||
{/* @ts-expect-error good, 'blah' is invalid */} | ||
<some-element otherProp="blah"/> | ||
|
||
{/* Additionally TypeScript will allow unknown dash-case props (as we didn't not define JS properties with these exact dash-cased names, React 19+ will set the element attributes, useful for setting the attributes but React has no way to specify to set attributes for names without dashes) */} | ||
<some-element some-prop="false" other-prop="foo"/> | ||
{/* @ts-expect-error foo doesn't exist. TypeScript will only check existence of properties without dashes */} | ||
<some-element foo="false"/> | ||
</>; | ||
}); | ||
}); | ||
export {}; | ||
//# sourceMappingURL=jsx-types-react.test.jsx.map |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import type { ElementAttributes } from './LumeElement.js'; | ||
declare class SomeElement extends HTMLElement { | ||
someProp: 'true' | 'false' | boolean; | ||
get otherProp(): number; | ||
set otherProp(_: this['__set__otherProp']); | ||
/** do not use this property, its only for JSX types */ | ||
__set__otherProp: number | 'foo'; | ||
} | ||
declare module 'solid-js' { | ||
namespace JSX { | ||
interface IntrinsicElements { | ||
'some-element': ElementAttributes<SomeElement, 'someProp' | 'otherProp'>; | ||
} | ||
} | ||
} | ||
export {}; | ||
//# sourceMappingURL=jsx-types-solid.test.d.ts.map |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* @jsxImportSource solid-js */ | ||
class SomeElement extends HTMLElement { | ||
someProp = true; | ||
get otherProp() { | ||
return 0; | ||
} | ||
set otherProp(_) { } | ||
/** do not use this property, its only for JSX types */ | ||
__set__otherProp; | ||
} | ||
SomeElement; | ||
describe('JSX types with ElementAttributes', () => { | ||
it('derives JSX types from classes', () => { | ||
; | ||
<> | ||
<some-element some-prop="false" other-prop="foo"/> | ||
<some-element some-prop="false" other-prop="foo"/> | ||
<some-element some-prop={false} other-prop={123}/> | ||
{/* @ts-expect-error good, number is invalid */} | ||
<some-element some-prop={123}/> | ||
{/* @ts-expect-error good, 'blah' is invalid */} | ||
<some-element other-prop="blah"/> | ||
|
||
{/* Additionally TypeScript will allow unknown dash-case props (the attr: can be used here to tell Solid to set the element attributes instead of the JS properties) */} | ||
<some-element attr:some-prop="false" attr:other-prop="foo"/> | ||
{/* @ts-expect-error foo doesn't exist. TypeScript will only check existence of properties without dashes */} | ||
<some-element foo="false"/> | ||
</>; | ||
}); | ||
}); | ||
export {}; | ||
//# sourceMappingURL=jsx-types-solid.test.jsx.map |
Oops, something went wrong.