Skip to content
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

feat(next): make defineConfig generic #12243

Merged
merged 21 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .changeset/forty-trains-notice.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
'astro': minor
---

Makes `defineConfig` generic
florian-lefebvre marked this conversation as resolved.
Show resolved Hide resolved

TypeScript will now provide feedback at the type level when using `defineConfig`. For example, it will make sure `i18n.locales` contains `i18n.defaultLocale`.

This also makes the `AstroUserConfig` generic, but both changes are backward compatible.
9 changes: 8 additions & 1 deletion packages/astro/config.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
type ViteUserConfig = import('vite').UserConfig;
type ViteUserConfigFn = import('vite').UserConfigFn;
type AstroUserConfig = import('./dist/types/public/config.js').AstroUserConfig;
type Locales = import('./dist/types/public/config.js').Locales;
type AstroInlineConfig = import('./dist/types/public/config.js').AstroInlineConfig;
type ImageServiceConfig = import('./dist/types/public/config.js').ImageServiceConfig;
type SharpImageServiceConfig = import('./dist/assets/services/sharp.js').SharpImageServiceConfig;
Expand All @@ -11,7 +12,13 @@ type EnvField = typeof import('./dist/env/config.js').envField;
* See the full Astro Configuration API Documentation
* https://astro.build/config
*/
export function defineConfig(config: AstroUserConfig): AstroUserConfig;
export function defineConfig<
TDefaultLocale extends string,
const TLocales extends [
TDefaultLocale | { codes: [TDefaultLocale, ...Array<string>]; path: string },
...Locales,
],
>(config: AstroUserConfig<TDefaultLocale, TLocales>): AstroUserConfig<TDefaultLocale, TLocales>;

/**
* Use Astro to generate a fully resolved Vite config
Expand Down
10 changes: 8 additions & 2 deletions packages/astro/src/config/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import type { UserConfig as ViteUserConfig } from 'vite';
import { Logger } from '../core/logger/core.js';
import { createRouteManifest } from '../core/routing/index.js';
import type { AstroInlineConfig, AstroUserConfig } from '../types/public/config.js';
import type { AstroInlineConfig, AstroUserConfig, Locales } from '../types/public/config.js';
import { createDevelopmentManifest } from '../vite-plugin-astro-server/plugin.js';

export function defineConfig(config: AstroUserConfig) {
export function defineConfig<
TDefaultLocale extends string,
const TLocales extends [
TDefaultLocale | { codes: [TDefaultLocale, ...Array<string>]; path: string },
...Locales,
],
>(config: AstroUserConfig<TDefaultLocale, TLocales>) {
return config;
}

Expand Down
30 changes: 25 additions & 5 deletions packages/astro/src/types/public/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ import type { AstroIntegration } from './integrations.js';

export type Locales = (string | { codes: string[]; path: string })[];

type NormalizeLocales<T extends Locales> = {
[K in keyof T]: T[K] extends string
? T[K]
: T[K] extends { codes: Array<string> }
? T[K]['codes'][number]
: never;
}[number];

export interface ImageServiceConfig<T extends Record<string, any> = Record<string, any>> {
entrypoint: 'astro/assets/services/sharp' | (string & {});
config?: T;
Expand Down Expand Up @@ -102,7 +110,13 @@ export interface ViteUserConfig extends OriginalViteUserConfig {
* Astro User Config
* Docs: https://docs.astro.build/reference/configuration-reference/
*/
export interface AstroUserConfig {
export interface AstroUserConfig<
TDefaultLocale extends string = never,
TLocales extends [
TDefaultLocale | { codes: [TDefaultLocale, ...Array<string>]; path: string },
...Locales,
] = never,
> {
/**
* @docs
* @kind heading
Expand Down Expand Up @@ -1214,7 +1228,7 @@ export interface AstroUserConfig {
*
* No particular language format or syntax is enforced, but we suggest using lower-case and hyphens as needed (e.g. "es", "pt-br") for greatest compatibility.
*/
defaultLocale: string;
defaultLocale: [TDefaultLocale] extends [never] ? string : TDefaultLocale;
/**
* @docs
* @name i18n.locales
Expand All @@ -1228,7 +1242,7 @@ export interface AstroUserConfig {
*
* No particular language code format or syntax is enforced, but your project folders containing your content files must match exactly the `locales` items in the list. In the case of multiple `codes` pointing to a custom URL path prefix, store your content files in a folder with the same name as the `path` configured.
*/
locales: Locales;
locales: [TLocales] extends [never] ? Locales : TLocales;

/**
* @docs
Expand Down Expand Up @@ -1258,7 +1272,11 @@ export interface AstroUserConfig {
* })
* ```
*/
fallback?: Record<string, string>;
fallback?: [TLocales] extends [never]
? Record<string, string>
: {
[Locale in NormalizeLocales<TLocales>]?: Exclude<NormalizeLocales<TLocales>, Locale>;
};

/**
* @docs
Expand Down Expand Up @@ -1444,7 +1462,9 @@ export interface AstroUserConfig {
*
* See the [Internationalization Guide](https://docs.astro.build/en/guides/internationalization/#domains) for more details, including the limitations of this feature.
*/
domains?: Record<string, string>;
domains?: [TLocales] extends [never]
? Record<string, string>
: Partial<Record<NormalizeLocales<TLocales>, string>>;
};

/** ! WARNING: SUBJECT TO CHANGE */
Expand Down
Loading