Unverified Commit 6ab52ed4 authored by Hannes Bornö's avatar Hannes Bornö Committed by GitHub

@next/font return types (#42753)

Currently the return type is the same when you declare a variable and
when you don't.
```tsx
const inter1 = Inter()
const inter2 = Inter({ variable: "--inter-font" })

inter1.variable // string | undefined
inter2.variable // string | undefined
```

With this change you get a type error if you try to access `.variable`
without providing a variable name.
```tsx
const inter1 = Inter()
const inter2 = Inter({ variable: "--inter-font" })

inter1.variable // Property 'variable' does not exist on type 'NextFont'.ts(2339)
inter2.variable // string
```

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have a helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the
feature request has been accepted for implementation before opening a
PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have a helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `pnpm build && pnpm lint`
- [ ] The "examples guidelines" are followed from [our contributing
doc](https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md)
parent 784d9a73
...@@ -1574,6 +1574,12 @@ ...@@ -1574,6 +1574,12 @@
], ],
"styles": ["normal"], "styles": ["normal"],
"axes": [ "axes": [
{
"tag": "slnt",
"min": -11,
"max": 11,
"defaultValue": 0
},
{ {
"tag": "wght", "tag": "wght",
"min": 200, "min": 200,
...@@ -2760,6 +2766,10 @@ ...@@ -2760,6 +2766,10 @@
"weights": ["400"], "weights": ["400"],
"styles": ["normal"] "styles": ["normal"]
}, },
"Fragment Mono": {
"weights": ["400"],
"styles": ["normal", "italic"]
},
"Francois One": { "Francois One": {
"weights": ["400"], "weights": ["400"],
"styles": ["normal"] "styles": ["normal"]
...@@ -3449,6 +3459,10 @@ ...@@ -3449,6 +3459,10 @@
"weights": ["100", "200", "300", "400", "500", "600", "700"], "weights": ["100", "200", "300", "400", "500", "600", "700"],
"styles": ["normal"] "styles": ["normal"]
}, },
"IBM Plex Sans JP": {
"weights": ["100", "200", "300", "400", "500", "600", "700"],
"styles": ["normal"]
},
"IBM Plex Sans KR": { "IBM Plex Sans KR": {
"weights": ["100", "200", "300", "400", "500", "600", "700"], "weights": ["100", "200", "300", "400", "500", "600", "700"],
"styles": ["normal"] "styles": ["normal"]
...@@ -4780,6 +4794,18 @@ ...@@ -4780,6 +4794,18 @@
"weights": ["400"], "weights": ["400"],
"styles": ["normal"] "styles": ["normal"]
}, },
"Marhey": {
"weights": ["300", "400", "500", "600", "700", "variable"],
"styles": ["normal"],
"axes": [
{
"tag": "wght",
"min": 300,
"max": 700,
"defaultValue": 400
}
]
},
"Markazi Text": { "Markazi Text": {
"weights": ["400", "500", "600", "700", "variable"], "weights": ["400", "500", "600", "700", "variable"],
"styles": ["normal"], "styles": ["normal"],
...@@ -4869,8 +4895,16 @@ ...@@ -4869,8 +4895,16 @@
"styles": ["normal"] "styles": ["normal"]
}, },
"Merienda": { "Merienda": {
"weights": ["400", "700"], "weights": ["300", "400", "500", "600", "700", "800", "900", "variable"],
"styles": ["normal"] "styles": ["normal"],
"axes": [
{
"tag": "wght",
"min": 300,
"max": 900,
"defaultValue": 400
}
]
}, },
"Merienda One": { "Merienda One": {
"weights": ["400"], "weights": ["400"],
...@@ -5547,8 +5581,16 @@ ...@@ -5547,8 +5581,16 @@
] ]
}, },
"Noto Sans Bassa Vah": { "Noto Sans Bassa Vah": {
"weights": ["400"], "weights": ["400", "500", "600", "700", "variable"],
"styles": ["normal"] "styles": ["normal"],
"axes": [
{
"tag": "wght",
"min": 400,
"max": 700,
"defaultValue": 400
}
]
}, },
"Noto Sans Batak": { "Noto Sans Batak": {
"weights": ["400"], "weights": ["400"],
...@@ -6106,6 +6148,35 @@ ...@@ -6106,6 +6148,35 @@
} }
] ]
}, },
"Noto Sans Lao Looped": {
"weights": [
"100",
"200",
"300",
"400",
"500",
"600",
"700",
"800",
"900",
"variable"
],
"styles": ["normal"],
"axes": [
{
"tag": "wdth",
"min": 62.5,
"max": 100,
"defaultValue": 100
},
{
"tag": "wght",
"min": 100,
"max": 900,
"defaultValue": 400
}
]
},
"Noto Sans Lepcha": { "Noto Sans Lepcha": {
"weights": ["400"], "weights": ["400"],
"styles": ["normal"] "styles": ["normal"]
...@@ -6234,6 +6305,10 @@ ...@@ -6234,6 +6305,10 @@
} }
] ]
}, },
"Noto Sans Mende Kikakui": {
"weights": ["400"],
"styles": ["normal"]
},
"Noto Sans Meroitic": { "Noto Sans Meroitic": {
"weights": ["400"], "weights": ["400"],
"styles": ["normal"] "styles": ["normal"]
...@@ -6300,8 +6375,16 @@ ...@@ -6300,8 +6375,16 @@
"styles": ["normal"] "styles": ["normal"]
}, },
"Noto Sans New Tai Lue": { "Noto Sans New Tai Lue": {
"weights": ["400"], "weights": ["400", "500", "600", "700", "variable"],
"styles": ["normal"] "styles": ["normal"],
"axes": [
{
"tag": "wght",
"min": 400,
"max": 700,
"defaultValue": 400
}
]
}, },
"Noto Sans Newa": { "Noto Sans Newa": {
"weights": ["400"], "weights": ["400"],
...@@ -7153,6 +7236,18 @@ ...@@ -7153,6 +7236,18 @@
} }
] ]
}, },
"Noto Serif Oriya": {
"weights": ["400", "500", "600", "700", "variable"],
"styles": ["normal"],
"axes": [
{
"tag": "wght",
"min": 400,
"max": 700,
"defaultValue": 400
}
]
},
"Noto Serif SC": { "Noto Serif SC": {
"weights": ["200", "300", "400", "500", "600", "700", "900"], "weights": ["200", "300", "400", "500", "600", "700", "900"],
"styles": ["normal"] "styles": ["normal"]
...@@ -8996,6 +9091,24 @@ ...@@ -8996,6 +9091,24 @@
"weights": ["400"], "weights": ["400"],
"styles": ["normal"] "styles": ["normal"]
}, },
"Sono": {
"weights": ["200", "300", "400", "500", "600", "700", "800", "variable"],
"styles": ["normal"],
"axes": [
{
"tag": "MONO",
"min": 0,
"max": 1,
"defaultValue": 1
},
{
"tag": "wght",
"min": 200,
"max": 800,
"defaultValue": 400
}
]
},
"Sonsie One": { "Sonsie One": {
"weights": ["400"], "weights": ["400"],
"styles": ["normal"] "styles": ["normal"]
...@@ -9985,7 +10098,7 @@ ...@@ -9985,7 +10098,7 @@
"styles": ["normal"] "styles": ["normal"]
}, },
"Zen Old Mincho": { "Zen Old Mincho": {
"weights": ["400", "700", "900"], "weights": ["400", "500", "600", "700", "900"],
"styles": ["normal"] "styles": ["normal"]
}, },
"Zen Tokyo Zoo": { "Zen Tokyo Zoo": {
......
This diff is collapsed.
/* eslint-disable @typescript-eslint/no-unused-vars */ /* eslint-disable @typescript-eslint/no-unused-vars */
import type { FontModule } from 'next/font' import type {
type Display = 'auto' | 'block' | 'swap' | 'fallback' | 'optional' CssVariable,
type CssVariable = `--${string}` Display,
type LocalFont = { NextFont,
NextFontWithVariable,
} from '../types'
type LocalFont<T extends CssVariable | undefined = undefined> = {
src: src:
| string | string
| Array<{ | Array<{
...@@ -16,10 +20,14 @@ type LocalFont = { ...@@ -16,10 +20,14 @@ type LocalFont = {
adjustFontFallback?: 'Arial' | 'Times New Roman' | false adjustFontFallback?: 'Arial' | 'Times New Roman' | false
fallback?: string[] fallback?: string[]
preload?: boolean preload?: boolean
variable?: CssVariable variable?: T
declarations?: Array<{ prop: string; value: string }> declarations?: Array<{ prop: string; value: string }>
} }
export default function localFont(options: LocalFont): FontModule { export default function localFont<
T extends CssVariable | undefined = undefined
>(
options: LocalFont<T>
): T extends undefined ? NextFont : NextFontWithVariable {
throw new Error() throw new Error()
} }
export type CssVariable = `--${string}`
export type Display = 'auto' | 'block' | 'swap' | 'fallback' | 'optional'
export type NextFont = {
className: string
style: { fontFamily: string; fontWeight?: number; fontStyle?: string }
}
export type NextFontWithVariable = NextFont & {
variable: string
}
export type FontModule = {
className: string
variable?: string
style: { fontFamily: string; fontWeight?: number; fontStyle?: string }
}
export type AdjustFontFallback = { export type AdjustFontFallback = {
fallbackFont: string fallbackFont: string
ascentOverride?: string ascentOverride?: string
......
...@@ -10,9 +10,7 @@ const fetch = require('node-fetch') ...@@ -10,9 +10,7 @@ const fetch = require('node-fetch')
let fontFunctions = `/** let fontFunctions = `/**
* This is an autogenerated file by scripts/update-google-fonts.js * This is an autogenerated file by scripts/update-google-fonts.js
*/ */
import type { FontModule } from 'next/font' import type { CssVariable, NextFont, NextFontWithVariable, Display } from '../types'
type Display = 'auto'|'block'|'swap'|'fallback'|'optional'
type CssVariable = \`--\${string}\`
` `
const fontData = {} const fontData = {}
for (let { family, fonts, axes, subsets } of familyMetadataList) { for (let { family, fonts, axes, subsets } of familyMetadataList) {
...@@ -59,7 +57,7 @@ const fetch = require('node-fetch') ...@@ -59,7 +57,7 @@ const fetch = require('node-fetch')
fontFunctions += `export declare function ${family.replaceAll( fontFunctions += `export declare function ${family.replaceAll(
' ', ' ',
'_' '_'
)}(options${optionalIfVariableFont}: { )}<T extends CssVariable | undefined = undefined>(options${optionalIfVariableFont}: {
weight${optionalIfVariableFont}:${formatUnion( weight${optionalIfVariableFont}:${formatUnion(
weightTypes weightTypes
)} | Array<${formatUnion( )} | Array<${formatUnion(
...@@ -67,7 +65,7 @@ const fetch = require('node-fetch') ...@@ -67,7 +65,7 @@ const fetch = require('node-fetch')
)}> )}>
style?: ${formatUnion(styleTypes)} | Array<${formatUnion(styleTypes)}> style?: ${formatUnion(styleTypes)} | Array<${formatUnion(styleTypes)}>
display?:Display display?:Display
variable?: CssVariable variable?: T
preload?:boolean preload?:boolean
fallback?: string[] fallback?: string[]
adjustFontFallback?: boolean adjustFontFallback?: boolean
...@@ -77,7 +75,7 @@ const fetch = require('node-fetch') ...@@ -77,7 +75,7 @@ const fetch = require('node-fetch')
? `axes?:(${formatUnion(optionalAxes.map(({ tag }) => tag))})[]` ? `axes?:(${formatUnion(optionalAxes.map(({ tag }) => tag))})[]`
: '' : ''
} }
}):FontModule }): T extends undefined ? NextFont : NextFontWithVariable
` `
} }
......
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