Skip to content

strictIndexSignatures appears to be broken since at least v11.0.0 #477

@benjamincburns

Description

@benjamincburns

Per the title, it seems the strictIndexSignatures option has no effect since v11.0.0.

The below script fetches the latest JSONSchema for NPM's package.json file and attempts to generate types for it. In spite of the strictIndexSignatures: true at line 20, I still get errors like the following:

lib/types.ts:15788:3 - error TS2411: Property 'require' of type 'PackageExportsEntry | PackageExportsFallback | undefined' is not assignable to 'string' index type 'PackageExportsEntry | PackageExportsFallback'.

15788   require?: PackageExportsEntry | PackageExportsFallback;

The script in question (depends on got to fetch the schema) is here:

import { resolve as pathResolve, join as pathJoin } from "path";
import { writeFileSync } from "fs";

import got from "got";
import { compile } from "json-schema-to-typescript";
import type { JSONSchema4 } from "json-schema";

const url =
  "https://raw.githubusercontent.com/SchemaStore/schemastore/master/src/schemas/json/package.json";
const outputPath = pathJoin(pathResolve(__dirname, "..", "lib"), "package-json-types.ts");

(async function () {
  try {
    console.log(`Fetching updated JSONSchema for package.json from ${url}`);
    const schema = (await got(url).json()) as JSONSchema4;

    console.log("Converting JSONSchema to TypeScript interfaces");
    const result = await compile(schema, "PackageJson", {
      unreachableDefinitions: true,
      strictIndexSignatures: true,
    });

    console.log(`Writing interfaces to ${outputPath}`);
    writeFileSync(outputPath, result, { encoding: "utf8" });
  } catch (err) {
    console.error(err);
    process.exit(1);
  }
})();

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions