Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
7 changes: 7 additions & 0 deletions .changeset/plugin-exact-optional-property-types.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@streamdown/code": patch
"@streamdown/math": patch
"@streamdown/mermaid": patch
---

Support `exactOptionalPropertyTypes`: add `| undefined` to optional props in the plugin option types so possibly-`undefined` values can be forwarded to `createCodePlugin`, `createMathPlugin` and `createMermaidPlugin` without type errors
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ jobs:
run: pnpm build:packages

- name: Check exactOptionalPropertyTypes compatibility
run: pnpm --filter streamdown exec tsc -p tsconfig.exact-optional.json
run: pnpm --filter streamdown --filter @streamdown/code --filter @streamdown/math --filter @streamdown/mermaid exec tsc -p tsconfig.exact-optional.json

- name: Build Website
run: pnpm build --filter website
Expand Down
4 changes: 2 additions & 2 deletions packages/streamdown-code/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export interface HighlightOptions {
* Whether the code block is still streaming. Its result is superseded by
* the next update, so it is not cached.
*/
isIncomplete?: boolean;
isIncomplete?: boolean | undefined;
language: BundledLanguage;
themes: [ThemeInput, ThemeInput];
}
Expand Down Expand Up @@ -75,7 +75,7 @@ export interface CodePluginOptions {
* Default themes for syntax highlighting [light, dark]
* @default ["github-light", "github-dark"]
*/
themes?: [ThemeInput, ThemeInput];
themes?: [ThemeInput, ThemeInput] | undefined;
}

const languageAliases = Object.fromEntries(
Expand Down
1 change: 1 addition & 0 deletions packages/streamdown-code/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
"scripts": {
"bench": "vitest bench --run > results.txt",
"build": "tsup",
"check-types:exact-optional": "pnpm build && tsc -p tsconfig.exact-optional.json",
"test": "vitest run",
"test:coverage": "vitest --coverage run"
},
Expand Down
8 changes: 8 additions & 0 deletions packages/streamdown-code/tsconfig.exact-optional.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"exactOptionalPropertyTypes": true
},
"include": ["type-tests/exact-optional-property-types.ts"],
"exclude": ["node_modules", "build", "dist"]
}
2 changes: 1 addition & 1 deletion packages/streamdown-code/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@
"noEmit": true
},
"include": ["**/*.ts", "**/*.tsx"],
"exclude": ["node_modules", "build", "dist"]
"exclude": ["node_modules", "build", "dist", "type-tests"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* Type-level regression test for `exactOptionalPropertyTypes` support.
*
* This file is type-checked with `tsconfig.exact-optional.json`
* (`pnpm check-types:exact-optional`) against the built declarations, with
* `exactOptionalPropertyTypes` enabled. It is never executed.
*/
import { createCodePlugin, type ThemeInput } from "../dist/index";

interface CodeOptions {
isIncomplete?: boolean;
themes?: [ThemeInput, ThemeInput];
}

// Forwarding possibly-`undefined` optional props must type-check.
export const createPlugin = (options: CodeOptions) =>
createCodePlugin({ themes: options.themes });

export const plugin = createCodePlugin({ themes: undefined });

export const highlight = (options: CodeOptions) =>
plugin.highlight({
code: "",
isIncomplete: options.isIncomplete,
language: "ts",
themes: plugin.getThemes(),
});
6 changes: 3 additions & 3 deletions packages/streamdown-math/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export interface MathPlugin {
/**
* Get CSS styles path for math rendering
*/
getStyles?: () => string;
getStyles?: (() => string) | undefined;
name: "katex";
/**
* Rehype plugin for rendering math
Expand All @@ -32,12 +32,12 @@ export interface MathPluginOptions {
* KaTeX error color
* @default "var(--color-muted-foreground)"
*/
errorColor?: string;
errorColor?: string | undefined;
/**
* Enable single dollar sign for inline math ($...$)
* @default false
*/
singleDollarTextMath?: boolean;
singleDollarTextMath?: boolean | undefined;
}

/**
Expand Down
1 change: 1 addition & 0 deletions packages/streamdown-math/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
],
"scripts": {
"build": "tsup",
"check-types:exact-optional": "pnpm build && tsc -p tsconfig.exact-optional.json",
"test": "vitest run",
"test:coverage": "vitest --coverage run"
},
Expand Down
8 changes: 8 additions & 0 deletions packages/streamdown-math/tsconfig.exact-optional.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"exactOptionalPropertyTypes": true
},
"include": ["type-tests/exact-optional-property-types.ts"],
"exclude": ["node_modules", "build", "dist"]
}
2 changes: 1 addition & 1 deletion packages/streamdown-math/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@
"noEmit": true
},
"include": ["**/*.ts", "**/*.tsx"],
"exclude": ["node_modules", "build", "dist"]
"exclude": ["node_modules", "build", "dist", "type-tests"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* Type-level regression test for `exactOptionalPropertyTypes` support.
*
* This file is type-checked with `tsconfig.exact-optional.json`
* (`pnpm check-types:exact-optional`) against the built declarations, with
* `exactOptionalPropertyTypes` enabled. It is never executed.
*/
import { createMathPlugin, type MathPlugin } from "../dist/index";

interface MathOptions {
errorColor?: string;
getStyles?: () => string;
singleDollarTextMath?: boolean;
}

// Forwarding possibly-`undefined` optional props must type-check.
export const createPlugin = (options: MathOptions) =>
createMathPlugin({
errorColor: options.errorColor,
singleDollarTextMath: options.singleDollarTextMath,
});

export const plugin = createMathPlugin({
errorColor: undefined,
singleDollarTextMath: undefined,
});

export const withStyles = (options: MathOptions): MathPlugin => ({
...plugin,
getStyles: options.getStyles,
});
2 changes: 1 addition & 1 deletion packages/streamdown-mermaid/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ export interface MermaidPluginOptions {
/**
* Default Mermaid configuration
*/
config?: MermaidConfig;
config?: MermaidConfig | undefined;
}

const defaultConfig: MermaidConfig = {
Expand Down
1 change: 1 addition & 0 deletions packages/streamdown-mermaid/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
],
"scripts": {
"build": "tsup",
"check-types:exact-optional": "pnpm build && tsc -p tsconfig.exact-optional.json",
"test": "vitest run",
"test:coverage": "vitest --coverage run"
},
Expand Down
8 changes: 8 additions & 0 deletions packages/streamdown-mermaid/tsconfig.exact-optional.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"exactOptionalPropertyTypes": true
},
"include": ["type-tests/exact-optional-property-types.ts"],
"exclude": ["node_modules", "build", "dist"]
}
2 changes: 1 addition & 1 deletion packages/streamdown-mermaid/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@
"noEmit": true
},
"include": ["**/*.ts", "**/*.tsx"],
"exclude": ["node_modules", "build", "dist"]
"exclude": ["node_modules", "build", "dist", "type-tests"]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/**
* Type-level regression test for `exactOptionalPropertyTypes` support.
*
* This file is type-checked with `tsconfig.exact-optional.json`
* (`pnpm check-types:exact-optional`) against the built declarations, with
* `exactOptionalPropertyTypes` enabled. It is never executed.
*/
import type { MermaidConfig } from "mermaid";
import { createMermaidPlugin } from "../dist/index";

interface MermaidOptions {
config?: MermaidConfig;
}

// Forwarding possibly-`undefined` optional props must type-check.
export const createPlugin = (options: MermaidOptions) =>
createMermaidPlugin({ config: options.config });

export const plugin = createMermaidPlugin({ config: undefined });
Loading