-
Notifications
You must be signed in to change notification settings - Fork 26.9k
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 typescript example #96
Comments
Custom webpack config would make this happen: #40 |
You don't need a custom webpack config (althouh it would be nice). There's nothing preventing you from using typescript as-is. For example: tsconfig.json: {
"compilerOptions": {
"module": "commonjs",
"target": "es5",
"jsx": "react",
"noImplicitAny": false,
"sourceMap": false
}
} As I don't think next.js has any typings (yet), you might need a next.d.ts: declare module 'next/head' {
import * as React from 'react';
export default class Head extends React.Component<any, any> {}
}
declare module 'next/link' {
import * as React from 'react';
export default class Link extends React.Component<any, any> {}
} That should get you rolling. You can use the same approach for other next specific modules. To make the workflow a little smoother you can run |
wow @rossipedia i have been doing literally this exact thing locally, and it works! it occurred to me moments after i opened this issue that it should be quite possible to layer typings are proving a little more insidious than that because the library injects |
When we add custom babel / webpack config, I would love to have a TypeScript example and a Flow example. @rossipedia thanks a lot for the temporary workaround. If you'd be up for documenting the temporary solution in the Wiki, I think a lot of people would appreciate it! |
If you are using VSCode it is also possible to hide derived JS file. |
@rauchg I'm traveling this weekend, but if nobody else gets to it I'd be happy to add a page to the wiki, I can whip it up during my flight tomorrow |
trying to get running with typescript - things were going great until I decided to configure the page header...
anyone else seeing this in a typescript project? |
Have you tried const Head = require('next/head') ? |
Of course On Nov 5, 2016, 6:10 AM -0700, Giacomo Rebonato notifications@github.com, wrote:
|
Ok, if you write it like this, it will work const Head = require('next/head').default |
I found it pretty easy to just edit the gulpfile to also compile .ts files. https://github.com/zeit/next.js/blob/master/gulpfile.js#L123 Why not compile .ts files with typescript and .js files with babel by default? |
With Webpack you mean? |
@skhavari your issue is a known module interop issue and adding '.default' is not needed. {
"compilerOptions": {
"target": "es6",
"moduleResolution": "node",
"jsx": "react"
},
"filesGlob": [
"pages/**/*.tsx"
]
} I also add 'tsc' in my scripts like so: ...
"dev": "tsc && next",
"build": "tsc && next build",
"start": "tsc && next start"
... |
@rossipedia I think the same can be achieve by having your own gulp file that compiles ts files and move them to the page dir where next picks them up and reloads them right? |
@gozes I don't see why not. I've rarely used gulp, but from what I know of it that seems like a fairly standard use case. |
So here's my attempt https://github.com/jagreehal/next-typescript It works fine apart from the page reloading twice as I don't think there is a way to configure files NOT to be watched by the hot-reloader. If additional ignore entries could be added here via next.config.js then a solution like this might work. |
@jagreehal That's really cool 💯 I'll have a look into adding the ignore config option in next.config.js. |
I made a PR to @jagreehal example to separate the ts and js. Now the ts is in |
@timneutkens thanks. Let me know if I can help adding config option in next.config.js. |
I have created a PR with a TypeScript example that works with just running |
@giacomorebonato awesome stuff! left you some review comments 🍰 |
Any suggestion on how to use TS with custom server? I wanted to use Express and have some trouble. Basically doing --edit-- |
With TS 2.1 and above (if I remember correct) you no longer need the typings, if it's in Update: Sorry for replying to such an old post, I didn't see the notification mail until now. Google Inbox grouped it together with another notification mail and I didn't notice the date. |
this looks awesome but I also really like TypeScript.
is there any way to integrate typescript into the workflow?
The text was updated successfully, but these errors were encountered: