Replies: 84 comments 27 replies
-
If you want to do something like this it's your own responsibility to compile next.config.ts to next.config.js. We're not planning to transpile next.config.js. |
Beta Was this translation helpful? Give feedback.
-
@virzak Yeah, not without much effort. It will require runtime transpilation w/ You can however, use |
Beta Was this translation helpful? Give feedback.
-
any changes here since nextJs Core is running with typescript? |
Beta Was this translation helpful? Give feedback.
-
Any chance this issue could be reconsidered? It was closed over a year ago and a lot has changed since then, TS support is now part of the core. A specific reason I would like to have The same |
Beta Was this translation helpful? Give feedback.
-
Still the same as #5318 (comment) Compiling it would slow down bootup and make config loading significantly more complex compared to the benefits. |
Beta Was this translation helpful? Give feedback.
-
I believe the main performance problem is production-related. What if |
Beta Was this translation helpful? Give feedback.
-
It's both development and production. Note that this file is a config file and generally shouldn't have anything complex in there. |
Beta Was this translation helpful? Give feedback.
-
It should just build and cache the config like everything else so it doesn't slow bootup performance. Plus, I would be shocked if it was a significant performance impact on bootup compared to the benefits. It's frustrating not being able to use a consistent language throughout my entire project and include the same linting tools or esnext syntax.
Umm, maybe for super simple use cases? I don't know. I work on big complex apps that require a lot of customization and tweaking. Data on this would be interesting. They certainly felt it was worthwhile to transpile One particularly useful tool for modifying configs imperatively like this that loses a lot of benefits without transpiling TS is Ramda. That's my FP toolbelt for everything because it has awesome TS support for dynamic type inference when currying. |
Beta Was this translation helpful? Give feedback.
-
Is supporting a transpiled |
Beta Was this translation helpful? Give feedback.
-
Generating a list of urls to export will be covered by |
Beta Was this translation helpful? Give feedback.
-
You probably already have the following 2 packages installed if you are using typescript with nextjs: Then you can use a function like the one below to require typescript file on the fly: function requireTypescript(path) {
const fileContent = require('fs').readFileSync(path, 'utf8')
const compiled = require('@babel/core').transform(
fileContent,
{
filename: path,
presets: [ '@babel/preset-typescript' ],
},
)
return eval(compiled.code)
} Then use it in your const myModule = requireTypescript('./path/to/mymodule.ts') Note: you will need to inject some path information if your ts file is not in the same folder of |
Beta Was this translation helpful? Give feedback.
-
Note about #5318 (comment)
|
Beta Was this translation helpful? Give feedback.
-
But if you decided to use typescript in your project, didn't you already trade in some performance for type safety? How come the performance impact is not seen as problematic when it happens on every change of a file, but is suddenly so insurmountable when it's once on boot? And If you cache it a bit smart it's not even happening on every boot. If the concern is that people would blame
That would make clear to people which impact the use of which tool has for them. |
Beta Was this translation helpful? Give feedback.
-
@Janpot because it's cached inside Next.js and not with this method. On top of that it affects production boot if you use |
Beta Was this translation helpful? Give feedback.
-
Yes, I agree, not with this method. But if next were to hypothetically implement this feature, I presume it wouldn't be too hard to add cache. Since next.js already has infrastructure to compile and cache typescript. And for production boot it could precompile when running |
Beta Was this translation helpful? Give feedback.
-
Can I suggest someone create a Discussions thread for this? There’s a fair amount of chatter here but the use case has been made quite clear already, so I’d prefer if this thread is kept to just updates regarding the implementation. |
Beta Was this translation helpful? Give feedback.
-
Here's a solution that works with type-safety, autocompletion, and possible to infer Code// @ts-check
/* eslint-disable @typescript-eslint/no-var-requires */
const { env } = require('./src/server/env'); // Optional - parses env vars with a zod-schema on build/start
/**
* Don't be scared of the generics here.
* All they do is to give us autocompletion when using this.
*
* @template {import('next').NextConfig} T
* @param {T} config - A generic parameter that flows through to the return type
* @constraint {{import('next').NextConfig}}
*/
function getConfig(config) {
return config;
}
/**
* @link https://nextjs.org/docs/api-reference/next.config.js/introduction
*/
module.exports = getConfig({
/**
* Dynamic configuration available for the browser and server.
* Note: requires `ssr: true` or a `getInitialProps` in `_app.tsx`
* @link https://nextjs.org/docs/api-reference/next.config.js/runtime-configuration
*/
publicRuntimeConfig: {
NODE_ENV: env.NODE_ENV,
},
}); Repo |
Beta Was this translation helpful? Give feedback.
-
I will add my use-case which I think is relevant to the discussion. Some of the key aspects of the multi-tenancy are:
Where many of these can be solve trough the I think for us the tradeoff of having a slower boot time vs high type-safe configurability, is something we're would be willing to sacrifice. Hope this help in understanding why we think is important adopt TypeScript for the NextJS configuration. |
Beta Was this translation helpful? Give feedback.
-
Just to add on to use cases. I'm querying redirects from our API. We already have a context setup for our API the sets up the client. It's all written in typescript. I'd like to be able to import it into our config without duplicating the client setup code. |
Beta Was this translation helpful? Give feedback.
-
Any update? In my use case, I need to import a |
Beta Was this translation helpful? Give feedback.
-
Not |
Beta Was this translation helpful? Give feedback.
-
Another option/workaround to consider for loading a
Then
|
Beta Was this translation helpful? Give feedback.
-
This change should help/fix (it's released in Node v19.6.0 and there is a backport to v18). |
Beta Was this translation helpful? Give feedback.
-
TailwindCSS added support for this in the 3.3 release https://tailwindcss.com/blog/tailwindcss-v3-3#esm-and-typescript-support. Would be great to be able to write Next config files in TypeScript |
Beta Was this translation helpful? Give feedback.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as off-topic.
-
Just wanted to note it appears to work for me using |
Beta Was this translation helpful? Give feedback.
-
Any update about using |
Beta Was this translation helpful? Give feedback.
-
https://github.com/devjiwonchoi/next-config-ts-preview This is an early access I've worked on, the PR #57656 is still pending to be reviewed by the team. If any issues, please file them here. To integrate into your working project, run the following commands:
NOTE: The following scripts are modifying the next package in your node_modules. To undo them, reinstall the dependencies. |
Beta Was this translation helpful? Give feedback.
-
cc @virzak This feature was shipped on
|
Beta Was this translation helpful? Give feedback.
-
Feature request
Is it possible to use next.config.ts instead of next.config.js?
Currently none of the typescript examples use typescript in the config file. Is it even possible?
@resir014
Beta Was this translation helpful? Give feedback.
All reactions