Skip to content

code-block.tsx: TokensResult fg/bg are optional in Shiki types, but TokenizedCode requires string (strictNullChecks error) #328

@t-koyoyo

Description

@t-koyoyo

Summary

In packages/elements/src/code-block.tsx, TokenizedCode defines fg/bg as required string, but codeToTokens returns TokensResult where fg/bg are optional (string | undefined). With strictNullChecks: true, this triggers a type error.

Steps to Reproduce

  1. Use packages/elements/src/code-block.tsx as-is.
  2. Enable strictNullChecks (or strict: true) in TS config.
  3. Type check the project.

Expected

No TypeScript error.

Actual

Type error:
Type 'string | undefined' is not assignable to type 'string'.

References

  • code-block.tsx: const tokenized: TokenizedCode = { tokens: result.tokens, fg: result.fg, bg: result.bg }
    Image
    Image
  • Shiki types: TokensResult.fg? / bg? are optional.
    Image

Possible Fixes

  • Make TokenizedCode use string | undefined for fg/bg
  • Or provide defaults (fg: result.fg ?? "inherit", bg: result.bg ?? "transparent")
  • Or use non-null assertion if intended

Environment

  • TypeScript: (e.g.) 5.x
  • Shiki: 3.21.0
  • strictNullChecks: true

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions