Skip to content

Commit dbe5973

Browse files
authored
[Example] with-typescript-types (#13746)
Closes [7882](#7882). Created as requested by @timneutkens I'm unsure if that's exactly what you wanted, so let me know what you want me to change and I'll do it asap.
1 parent d1c1612 commit dbe5973

File tree

8 files changed

+97
-0
lines changed

8 files changed

+97
-0
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
This example shows how to integrate the TypeScript type system into Next.js. Since TypeScript is supported out of the box with Next.js, all we have to do is to install TypeScript.
2+
3+
This example shows how to properly export and import typescript types without getting the
4+
5+
```js
6+
Attempted import error: 'TypeA' is not exported from './package-1'.
7+
```
8+
9+
error as raised in [vercel/next.js#7882](https://github.com/vercel/next.js/issues/7882).
10+
11+
## Useful links
12+
13+
[Add import type and export type support to TypeScript](https://github.com/babel/babel/pull/11171)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
/// <reference types="next" />
2+
/// <reference types="next/types/global" />
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"name": "with-typescript-types",
3+
"version": "1.0.0",
4+
"main": "index.js",
5+
"scripts": {
6+
"dev": "next",
7+
"build": "next build",
8+
"start": "next start",
9+
"type-check": "tsc"
10+
},
11+
"dependencies": {
12+
"next": "latest",
13+
"react": "^16.12.0",
14+
"react-dom": "^16.12.0"
15+
},
16+
"devDependencies": {
17+
"@types/node": "^12.12.21",
18+
"@types/react": "^16.9.16",
19+
"@types/react-dom": "^16.9.4",
20+
"typescript": "3.7.3"
21+
},
22+
"license": "MIT"
23+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import Document, { Head, Main, NextScript } from 'next/document'
2+
3+
export default class MyDocument extends Document {
4+
render() {
5+
return (
6+
<html>
7+
<Head>
8+
<style dangerouslySetInnerHTML={{ __html: this.props.css }} />
9+
</Head>
10+
<body>
11+
<Main />
12+
<NextScript />
13+
</body>
14+
</html>
15+
)
16+
}
17+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import type { TypeA } from '../types'
2+
3+
const Example: TypeA = {
4+
name: 'next',
5+
}
6+
7+
export default function Home() {
8+
return (
9+
<div style={{ display: 'flex', justifyContent: 'center' }}>
10+
<h1>
11+
<p>{Example.name}</p>
12+
</h1>
13+
</div>
14+
)
15+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"compilerOptions": {
3+
"allowJs": true,
4+
"alwaysStrict": true,
5+
"esModuleInterop": true,
6+
"forceConsistentCasingInFileNames": true,
7+
"isolatedModules": true,
8+
"jsx": "preserve",
9+
"lib": ["dom", "es2017"],
10+
"module": "esnext",
11+
"moduleResolution": "node",
12+
"noEmit": true,
13+
"noFallthroughCasesInSwitch": true,
14+
"noUnusedLocals": true,
15+
"noUnusedParameters": true,
16+
"resolveJsonModule": true,
17+
"skipLibCheck": true,
18+
"strict": true,
19+
"target": "esnext"
20+
},
21+
"exclude": ["node_modules"],
22+
"include": ["**/*.ts", "**/*.tsx"]
23+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export type { TypeA } from './package-1'
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export type TypeA = {
2+
name: string
3+
}

0 commit comments

Comments
 (0)