-
Notifications
You must be signed in to change notification settings - Fork 27.8k
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
TypeScript example proposal #845
Conversation
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.
this is a great minimal example! just a few suggestions for a simpler setup.
@@ -0,0 +1,5 @@ | |||
{ | |||
"dependencies": { | |||
"react": "registry:npm/react#15.0.1+20170104200836" |
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.
i would suggest using @types/react
directly in NPM instead of relying on typings
.
it's the way of the future, and you wouldn't need postinstall
task.
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.
Thanks, didn't know!
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.
If you make this change I'll merge!
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.
it's there! Is it what you mean right?
@@ -0,0 +1,17 @@ | |||
{ | |||
"scripts": { | |||
"dev": "$(npm bin)/concurrently \"tsc --watch\" \"next \"", |
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.
shouldn't need to add $(npm bin)/
in package.json scripts--NPM configures PATH
automatically for you when running npm scripts.
should be safe to just say "concurrently 'tsc --watch' 'next'"
"dependencies": { | ||
"next": "^1.2.3", | ||
"react": "^15.4.2", | ||
"typescript": "^2.1.5", |
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.
i'd argue that typescript is a devDependency
Hey @giacomorebonato I'm trying a similar experiment see https://github.com/jagreehal/next-typescript . You're going to have the problem of the page refreshing twice until Next v2.1 |
This wont be able to handle static content either if you want to separate the build and source. I will try to create a simple example using tscomp for the build process, which hopefully will be really neat! |
Looking forward to it @OskarKlintrot |
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.
Cool, though I wonder if we can use webpack here 🤔
A setup like this works pretty well with tests without webpack. |
As a Typescript enthusiast I feel freedom in just using the native compiler, without clashing on other JS build processes. |
}, | ||
"dependencies": { | ||
"next": "^1.2.3", | ||
"react": "^15.4.2" |
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.
A concern is that this will get out of sync with next's react
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.
I have remove React dependency. I thought it was a peer one.
"dev": "concurrently 'tsc --watch' 'next'" | ||
}, | ||
"dependencies": { | ||
"next": "^1.2.3" |
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.
This doesn't seem to depend on a specific version of next. Can you use latest
here?
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.
Sure.
}, | ||
"devDependencies": { | ||
"@types/react": "^15.0.1", | ||
"babel-preset-react": "^6.22.0", |
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 the babel preset? Is that a TypeScript requirement?
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.
I thought I was getting an error without it... but I have tried again and it's not needed.
Removed.
Now I have an example of using |
@OskarKlintrot nice I'll take a look. I've also been thinking about adding a babel-loader!ts-loader here https://github.com/zeit/next.js/blob/master/server/build/webpack.js |
In VSCode it's possible to hide the .js files and see only the .ts ones. |
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.
this seems like an ideal minimal typescript example: it provides a clean solution for the single problem of typescript compilation. it does not presume to handle more than it needs to, such as testing (solved by dozens of other tools; i suggest ts-jest) or static assets (supported by next itself).
"module": "commonjs", | ||
"target": "es6", | ||
"noImplicitAny": false, | ||
"sourceMap": false, |
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 not using source maps?
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.
You are right, they are useful. Will change this
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.
might be best to make this file as simple as possible, the bare minimum to compile successfully. let other folks choose their compiler flags as necessary.
just set jsx
, module
, and target
.
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.
Makes sense!
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.
(and maybe change es6
to es2015
while your at it ;-) )
Just did a PR to @giacomorebonato fork :) |
hi @OskarKlintrot I have seen you PR. |
It's bad practice to version control build files, that's the reason behind
gitignore. Ignoring *.js will probably not be a good idea in the other
examples...
Using stateless functions is [the preferred way of doing it in react](
https://facebook.github.io/react/blog/2015/09/10/react-v0.14-rc1.html#stateless-function-components)
so why do something different in the example?
Den mån 23 jan. 2017 18:51Giacomo Rebonato <[email protected]> skrev:
… hi @OskarKlintrot <https://github.com/OskarKlintrot> I have seen you PR.
I don't see .gitignore in the other examples.
I am willing to convert one component to a functional one, but I will keep
the other extending React.Component, since this is not a boilerplate but
just a small example. ;)
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#845 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/AGeDh-vmFTvWe4XfqNPbVyajxXNO2WBnks5rVOiBgaJpZM4LpQ6r>
.
|
ah ignoring |
@OskarKlintrot I just added another commit. In the gitignore I have considered only the .js files since .next and node_modules are excluded by top level .gitignore. |
You really kill all joy and motivation of helping out code wise when you take my changes but ignore my commits (PR), thous taking away all the credits for what I have actually done and leaving me out as a contributor. |
Maybe it was a tooling issue! Would love to have you both as contributors to have the best possible TypeScript example 😇 |
Thank you @rauchg, really appreciating it! I still see stuff to improve on (this example won't run on Windows, at least not on PS, for example), maybe I can calm myself down a bit and do a new PR later today. Changing subject, this commit history is really growing out of proportion, 13 commits and counting! The commits are longer than the code! 🙀 |
@OskarKlintrot sorry... I didn't want to disrespect you. I was just hoping to have this merged soon and it was a rookie mistake. I reverted and merged your PR, if you have other improvements you are welcome ;) |
Removed typings Moved TypeScript to dev dependencies removed unused typings changed react typings version removed react dependency set next dependency version to latest removed not needed babel preset enabled sourcemaps simplified tsconfig modified component to be a functional one
I was trying to squash the commits... and I did a mess, trying to repair. |
The commit history looks much better! I can make a new PR in a couple of minutes! |
merged! |
@@ -0,0 +1,13 @@ | |||
{ | |||
"scripts": { | |||
"dev": "concurrently \"tsc --watch\" \"next\"" |
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.
Does next
need to be quoted? Since it doesn't have any options. 🤔
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.
I just tried it and no, it's not needed. Is it a deal breaker we should fix or should we prioritize a cleaner commit history?
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.
@OskarKlintrot You could rebase this into the other "cleanup" commit 🥇
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.
True! It's my commit but on @giacomorebonato fork that I don't have push access to, can @giacomorebonato rebase it or do I have to do it and make a new PR to that fork? This is a bit outside my comfort zone with git.
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.
I will try to make the change and then rebase.
I have noticed problems when using |
What a huge PR has this gotten into 😅, I'll check that error @giacomorebonato |
Also, there is no README.md 😥 |
Hi @timneutkens I just came back from holidays. I will update this PR soon with a README.md too! |
examples/with-typescript/README.md
Outdated
Output JS files are aside the related TypeScript ones. | ||
|
||
## To fix | ||
It tsconfig.json the options `jsx="react"` compiles JSX syntax into nested React.createElement calls. |
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.
Typo:
In
tsconfig.json
the option"jsx": "react"
compiles JSX syntax into nested React.createElement calls.
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.
Looks good 👍
Thanks! I hope to investigate more and to help! |
Hi @giacomorebonato - How do you get sourcemaps to work reliably here? Runtime errors point to the transpiled JS code instead of the ts source files. Is there any workaround? I've tried enabling sourceMaps in tsconfig. |
@karthikiyengar i'm no expert in next's webpack setup, but typically you need to add |
Latest release of TypeScript compiles JSX files to JS. I want to try to edit the boilerplate in a way that we rely only on webpack for compilation and this should help also for the sourcemaps. |
I have mad a pull request where tsx compiles directly to: JS #1600 |
I wrote a game for a talk on Typed JavaScript and used TypeScript and Next
- https://github.com/jagreehal/typescript-react-redux-jest-card-game
Hopefully this helps.
…On Sun, Apr 2, 2017 at 9:00 PM, Rune Jeppesen ***@***.***> wrote:
I have mad a pull request where tsx compiles directly to: JS #1600
<#1600>
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#845 (comment)>, or mute
the thread
<https://github.com/notifications/unsubscribe-auth/ADd22k6xwJr5daUTgO3UVEYbMZk8lyJAks5rr_5QgaJpZM4LpQ6r>
.
|
Added a very basic example using TypeScript.
Livereload seems to work.
To test it just run
Using
concurrently
to run both next and TypeScript compiler with one command.Fixes #96