Skip to main content

SKY-Q413 · Quality, structure and performance

Referenced optional peer dependency

Low

A package marks a peer dependency optional in peerDependenciesMeta, but its own source imports that peer with a static runtime import. The package lists it in neither dependencies nor devDependencies.

Languages
TypeScript, JavaScript, package.json
Turned on by
skylos . --quality
Quality gate
counted toward the gate limits (full and diff scans). Reported as quality; counts toward max_quality.
Finding type
review
Wrong result? Report itA false positive, a missed case or a confusing message: it goes privately to the founder.

Why it matters

Consumers may legitimately skip an optional peer. A static import then fails when the module loads, so the optional contract is broken for exactly those consumers.

What Skylos flags

package.json · JSON
{  "name": "ui-kit",  "peerDependencies": {"react": "^18.0.0"},  "peerDependenciesMeta": {"react": {"optional": true}}}
src/index.ts · TypeScriptReported on line 1
import React from "react";export const version = React.version;

What Skylos leaves alone

package.json: same as in the flagged example.

src/index.ts · TypeScript
export async function loadReact() {  try {    return await import("react");  } catch {    return null;  }}

Why Skylos leaves it alone

The optional peer is loaded lazily, and the caller handles its absence.

How Skylos decides

  1. Reads peerDependencies and peerDependenciesMeta from the owning package.json, ignoring a peer that is also in dependencies or devDependencies.
  2. Considers only static runtime import and export-from declarations in owned sources, components and MDX. Type-only imports, declaration files and dynamic import or require calls stay quiet.
  3. Reports LOW once per peer at its first import. Packages with subpath exports are skipped, because a framework-specific entry point may require the peer; the receipt lists them under optional_peer_limitations.

How to fix it

Load the peer lazily and handle its absence, move the import to a subpath entry point, or make the package a regular dependency.

src/index.ts · TypeScript · the fix, against the flagged file
Removed: import React from "react";Removed: export const version = React.version;Added: export async function loadReact() {Added:   try {Added:     return await import("react");Added:   } catch {Added:     return null;Added:   }Added: }

When it can be wrong

  • A module that is only ever loaded by consumers that install the peer (for example through a deep import path) is still reported when the package has no subpath exports.

Seeing a case that isn't listed here? Report the wrong result so the rule can be fixed. Don't paste company code: describe the shape of it.

How to silence it

  • Set [tool.skylos] ignore = ["SKY-Q413"] to disable this rule project-wide.

  • Put the marker on the reported import line.

    TypeScript
    import React from "react"; // skylos: ignore[SKY-Q413] only loaded by React consumers

From the Skylos rule handbook, Skylos 5.8.0 (commit 5b880b0). All rules.