Unverified Commit 353e5051 authored by Joe Haddad's avatar Joe Haddad Committed by GitHub

Fix TypeScript typings (#7648)

* Fix TypeScript typing
This correctly defines `process.browser` instead of `process.env.browser`.

It also removes `process.crossOrigin` because it's not found in our documentation anywhere and is mostly used for internal purposes.

* Make sure `process.browser` doesn't throw a type error

* Split globals from Next.js module

* Allow `test` as a valid NODE_ENV

* Fix type assignment

* Make sure global types get published

* Fix test
parent 2debe7fb
...@@ -84,7 +84,7 @@ if (args['--help']) { ...@@ -84,7 +84,7 @@ if (args['--help']) {
} }
const defaultEnv = command === 'dev' ? 'development' : 'production' const defaultEnv = command === 'dev' ? 'development' : 'production'
process.env.NODE_ENV = process.env.NODE_ENV || defaultEnv ;(process.env as any).NODE_ENV = process.env.NODE_ENV || defaultEnv
// this needs to come after we set the correct NODE_ENV or // this needs to come after we set the correct NODE_ENV or
// else it might cause SSR to break // else it might cause SSR to break
......
...@@ -252,7 +252,7 @@ export async function verifyTypeScriptSetup(dir: string): Promise<void> { ...@@ -252,7 +252,7 @@ export async function verifyTypeScriptSetup(dir: string): Promise<void> {
} }
if (parsedTsConfig.include == null) { if (parsedTsConfig.include == null) {
appTsConfig.include = ['**/*.ts', '**/*.tsx'] appTsConfig.include = ['next-env.d.ts', '**/*.ts', '**/*.tsx']
} }
if (messages.length > 0) { if (messages.length > 0) {
...@@ -286,7 +286,10 @@ export async function verifyTypeScriptSetup(dir: string): Promise<void> { ...@@ -286,7 +286,10 @@ export async function verifyTypeScriptSetup(dir: string): Promise<void> {
if (!fs.existsSync(appTypeDeclarations)) { if (!fs.existsSync(appTypeDeclarations)) {
fs.writeFileSync( fs.writeFileSync(
appTypeDeclarations, appTypeDeclarations,
`/// <reference types="next" />${os.EOL}` '/// <reference types="next" />' +
os.EOL +
'/// <reference types="next/types/global" />' +
os.EOL
) )
} }
} }
...@@ -36,7 +36,8 @@ ...@@ -36,7 +36,8 @@
"router.d.ts", "router.d.ts",
"amp.js", "amp.js",
"amp.d.ts", "amp.d.ts",
"types/index.d.ts" "types/index.d.ts",
"types/global.d.ts"
], ],
"bin": { "bin": {
"next": "./dist/bin/next" "next": "./dist/bin/next"
......
/// <reference types="node" />
// Extend the NodeJS namespace with Next.js-defined properties
declare namespace NodeJS {
interface Process {
readonly browser: boolean
}
interface ProcessEnv {
readonly NODE_ENV: 'development' | 'production' | 'test'
}
}
/// <reference types="node" />
/// <reference types="react" />
/// <reference types="react-dom" />
import React from 'react' import React from 'react'
import { import {
...@@ -7,18 +11,6 @@ import { ...@@ -7,18 +11,6 @@ import {
NextApiRequest, NextApiRequest,
} from 'next-server/dist/lib/utils' } from 'next-server/dist/lib/utils'
/// <reference types="node" />
/// <reference types="react" />
/// <reference types="react-dom" />
declare namespace NodeJS {
interface ProcessEnv {
readonly NODE_ENV: 'development' | 'production'
readonly browser: boolean
readonly crossOrigin?: string
}
}
// Extend the React types with missing properties // Extend the React types with missing properties
declare module 'react' { declare module 'react' {
// <html amp=""> support // <html amp=""> support
......
...@@ -3,6 +3,7 @@ import { hello } from '../components/hello' ...@@ -3,6 +3,7 @@ import { hello } from '../components/hello'
import { World } from '../components/world' import { World } from '../components/world'
export default function HelloPage(): JSX.Element { export default function HelloPage(): JSX.Element {
console.log(process.browser)
return ( return (
<div> <div>
{hello()} <World /> {hello()} <World />
......
...@@ -4,5 +4,5 @@ ...@@ -4,5 +4,5 @@
"module": "esnext", "module": "esnext",
"jsx": "preserve" "jsx": "preserve"
}, },
"include": ["components", "pages"] "include": ["next-env.d.ts", "components", "pages"]
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment