-
Notifications
You must be signed in to change notification settings - Fork 156
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
fix: TS types on Router children components #395
base: main
Are you sure you want to change the base?
Conversation
After reading preactjs/preact#1180 (comment) I tried bumping the TS version as it mentioned the lowest working version was Edit: Though according to the I did elect to go with |
I suppose this PR does not address the issue mentioned in #269. Is my assumption correct?
I personally would not have any problems with having this PR accepted, but would it maybe be possible to only add the routable properties to components that are actually the child of a This could enable the following behavior, which I consider desirable: return (
<div>
<Home path="/home" /> // type error
</div>
)
return (
<Router>
<Home path="/home" /> // ok
</Router>
) |
@rschristian I'm in no way a TypeScript expert, so it's quite possible that I have misconfigured something and you are indeed correct. However, with your changes applied, I can now add the return (
<div>
<Home path="/home" /> // ok, but incorrect
</div>
)
return (
<Router>
<Home path="/home" /> // ok
</Router>
)
I don't think so. The merged PR that is visible there is on the main preactjs/preact repo and does not seem to directly solve the problems brought up in that issue. |
@leotaku Ah, yeah, now that you say that it rings a bell (sorry, this was made months ago). I don't believe it's possible to add arbitrary props to children only, no. FWIW, I think this style (using This just allows you to use non-const function components as routes in TS without it throwing a fit. This just allows the same behaviour that you'd get in JS. |
@rschristian Yeah, that makes sense. I just thought that, if something like what I proposed were possible, this PR would be a good place to implement it. Thanks for indulging me. |
Definitely. And if someone knows of a way to do that, I'd love to hear it. Would be a really nice improvement. |
Seems like typescript is not supported well at moment for this library ... |
@mx51damon It's supported, you just have to use slightly different syntax is all. preactjs-templates/typescript shows how to use TS with this library. This PR just opens up an alternative syntax that is far less type safe and should probably be avoided if I'm being honest. Edit: To expound, take the following: function Home() {
return <h1>Hello World</h1>
}
function App() {
return (
<Router>
<Home path="/" />
</Router>
);
} This is perfectly fine and normal in JS land, and is what this PR allows for TS. However, what you're doing is arbitrarily adding a new prop However, it makes sense that doing the following in TS shouldn't necessarily throw an error, even if it's not type safe, as this is behavior the library supports. |
Thanks man for the detailed explanation and I will give it a shot. 🙌 |
Thanks Ryan, I think it was not working before was because of I setup my webpack dev Server in a wrong way: by using this:
And now I changed to this:
|
This PR is a correction to the types when using components in place of the
Route
component as children ofRouter
.I added a new test which will cover the functionality that this adds. Before this change it was not possible to use anything but a type annotated const function as a child of
Router
. The attributespath
anddefault
would not be valid.With this change, the following becomes viable once more: