CodeSampleX

Ejemplo

typescript 7.0.2: createKeywordTypeNode

Muestra verificada para npm typescript 7.0.2: createKeywordTypeNode. El contrato se ejecutó en node 22 · linux alpine/x64 · docker y pasó.

sha256:280962ab78506c25faf0d1faac87e4c06e18a043e4674d674eee0e0de873e58f

Esta red ofrece una sola cosa: una muestra que compila. La ejecutó en un sandbox y guardó el recibo firmado. No califica ni garantiza nada: si el mismo código compila donde estás no es algo que haya medido. Cuántas claves de firma distintas presentaron un recibo de contrato aprobado. Una es solo el autor; más de una significa que alguien más también lo compiló. Una clave se genera sola y no tiene identidad registrada detrás, así que cuenta claves, no personas. MIT-0

Evidencia de ejecución

El entorno declarado y las ejecuciones firmadas se muestran por separado, para que veas exactamente qué ejecutó esta muestra y dónde.

Base de evidencia
Contrato firmado aprobado
Recibos de verificación
1
Claves de firma que lo compilaron
1
Entorno declarado linux 24 · ubuntu · glibc 2.39 x64 npm

Entornos de las ejecuciones de verificación

Entorno Contrato Etapas Ejecución
node 22 · linux alpine/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · node-typescript@1node:22-alpine@sha256:c610fcdfb1d5…
2026-08-28

Caso

HOW
Objetivo
verify typescript.createKeywordTypeNode in pkg:npm/typescript@7.0.2
Paquetes
Símbolos
  • typescript.createKeywordTypeNode
Creado
2026-08-28T07:22:30Z

Contrato

  1. createKeywordTypeNode constructs keyword type nodes for primitive TypeScript types (string, number, boolean, void, any, unknown, never)
  2. createKeywordType creates AST nodes for string names and SyntaxKind values using createKeywordTypeNode
  3. isKeywordType validates AST nodes as keyword type nodes and checks expected SyntaxKind
  4. getKeywordTypeName extracts human-readable keyword names and throws TypeError for non-keyword nodes

Archivos

  • PROMPT.md
  • csx.json
  • package-lock.json
  • package.json
  • spec.json
  • src/index.mjs
  • test/contract.mjs

Descargar el artefacto de código fuente (tar.gz)

Código fuente

PROMPT.md
Clean-room public code sample — generation instructions

Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.

A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.

Goal: verify typescript.createKeywordTypeNode in pkg:npm/typescript@7.0.2
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:npm/typescript@7.0.2
Demonstrate these symbols/APIs:
  - typescript.createKeywordTypeNode

Rules:
  - One focused purpose; the smallest project that proves the goal.
  - Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
  - Pin every dependency with a lockfile so resolution is reproducible.
  - No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
  - No personal names, emails, company names, or project identifiers of any kind.
  - No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
  - Keep it under 200 files and 256KB packed.
csx.json
{"case":{"caseId":"case:sha256:4ce87bff32ed183d73f3ca1e550382cece234fe134367abcc5c6446fccf3d41f","contract":["createKeywordTypeNode constructs keyword type nodes for primitive TypeScript types (string, number, boolean, void, any, unknown, never)","createKeywordType creates AST nodes for string names and SyntaxKind values using createKeywordTypeNode","isKeywordType validates AST nodes as keyword type nodes and checks expected SyntaxKind","getKeywordTypeName extracts human-readable keyword names and throws TypeError for non-keyword nodes"],"goal":"verify typescript.createKeywordTypeNode in pkg:npm/typescript@7.0.2","kind":"HOW","packages":["pkg:npm/typescript@7.0.2"],"schemaVersion":1,"symbols":["typescript.createKeywordTypeNode"]},"contractCommand":["node","test/contract.mjs"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"npm","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"npm","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/typescript@7.0.2"],"schemaVersion":1,"subject":"pkg:npm/typescript@7.0.2","symbols":["typescript.createKeywordTypeNode"],"verifierAdapter":"node-typescript@1"}
package-lock.json
{
  "name": "typescript-keyword-type-node-sample",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "typescript-keyword-type-node-sample",
      "version": "1.0.0",
      "license": "MIT-0",
      "dependencies": {
        "typescript": "7.0.2"
      }
    },
    "node_modules/@typescript/typescript-aix-ppc64": {
      "version": "7.0.2",
      "resolved": "https://registry.npmjs.org/@typescript/typescript-aix-ppc64/-/typescript-aix-ppc64-7.0.2.tgz",
      "integrity": "sha512-MTKKkWB7p/0E9xi1d1tHtZ5PiLkGEMIq88pK2CubZjOsLtYTLqhgIgi6zepFa+9GHZ6h05NMCkQxGKiPXMxXtQ==",
      "cpu": [
        "ppc64"
      ],
      "license": "Apache-2.0",
      "optional": true,
      "os": [
        "aix"
      ],
      "engines": {
        "node": ">=16.20.0"
      }
    },
    "node_modules/@typescript/typescript-darwin-arm64": {
      "version": "7.0.2",
      "resolved": "https://registry.npmjs.org/@typescript/typescript-darwin-arm64/-/typescript-darwin-arm64-7.0.2.tgz",
      "integrity": "sha512-gowzar9MwS/aRWp6f3a4KUqzRjAZjOsmGNCM6LcTgXum+dBfgsBVMN+AgvOCCbguXyick6LJhpBszxMebJ8syA==",
      "cpu": [
        "arm64"
      ],
      "license": "Apache-2.0",
      "optional": true,
      "os": [
        "darwin"
      ],
      "engines": {
        "node": ">=16.20.0"
      }
    },
    "node_modules/@typescript/typescript-darwin-x64": {
      "version": "7.0.2",
      "resolved": "https://registry.npmjs.org/@typescript/typescript-darwin-x64/-/typescript-darwin-x64-7.0.2.tgz",
      "integrity": "sha512-SZ9xZInqApNlNGc9s0W1VSsktYSOe9cFqNOIqmN1Gs8SmkjKZYFt017G4VwPxASInODuAdbTW7sXiFUf893RgA==",
      "cpu": [
        "x64"
      ],
      "license": "Apache-2.0",
      "optional": true,
      "os": [
        "darwin"
      ],
      "engines": {
        "node": ">=16.20.0"
      }
    },
    "node_modules/@typescript/typescript-freebsd-arm64": {
      "version": "7.0.2",
      "resolved": "https://registry.npmjs.org/@typescript/typescript-freebsd-arm64/-/typescript-freebsd-arm64-7.0.2.tgz",
      "integrity": "sha512-W5NH4y/J0plIIS5b2xvTEkU7JFxyqdMAOgf+Ilhl0vHQXKO5dZoxd+C/jEtq56c4F3wk71RB4BMRQ2XdI+bwYQ==",
      "cpu": [
        "arm64"
      ],
      "license": "Apache-2.0",
      "optional": true,
      "os": [
        "freebsd"
      ],
      "engines": {
        "node": ">=16.20.0"
      }
    },
    "node_modules/@typescript/typescript-freebsd-x64": {
      "version": "7.0.2",
      "resolved": "https://registry.npmjs.org/@typescript/typescript-freebsd-x64/-/typescript-freebsd-x64-7.0.2.tgz",
      "integrity": "sha512-UMGDx5sTpzNw3WiPebH7l90IWfJggEd+egHt/q6p7/Cm3zqoV7VxkGXt+3DxPIw8CcmvAB0j3sVVfbhX+M4Tpw==",
      "cpu": [
        "x64"
      ],
      "license": "Apache-2.0",
      "optional": true,
      "os": [
        "freebsd"
      ],
      "engines": {
        "node": ">=16.20.0"
      }
    },
    "node_modules/@typescript/typescript-linux-arm": {
      "version": "7.0.2",
      "resolved": "https://registry.npmjs.org/@typescript/typescript-linux-arm/-/typescript-linux-arm-7.0.2.tgz",
      "integrity": "sha512-gffT3xPz9sR7j/YJExkyPntrI0P2EP9XbOyWzth2/Gs0RstK+90RBcO0ncXoXy/beYll1SXw846Nf2zdnEz0QQ==",
      "cpu": [
        "arm"
      ],
      "license": "Apache-2.0",
      "optional": true,
      "os": [
        "linux"
      ],
      "engines": {
        "node": ">=16.20.0"
      }
    },
    "node_modules/@typescript/typescript-linux-arm64": {
      "version": "7.0.2",
      "resolved": "https://registry.npmjs.org/@typescript/typescript-linux-arm64/-/typescript-linux-arm64-7.0.2.tgz",
      "integrity": "sha512-Qh4eU4/y3yDjnfjjyPYihMj5/ODIlmt+Bzu17OI+fiSRDW57QmU5SiN63exPRNJPKUzcc1INa1NXdrJ+MqHjUQ==",
      "cpu": [
        "arm64"
      ],
      "license": "Apache-2.0",
      "optional": true,
      "os": [
        "linux"
      ],
      "engines": {
        "node": ">=16.20.0"
      }
    },
    "node_modules/@typescript/typescript-linux-loong64": {
      "version": "7.0.2",
      "resolved": "https://registry.npmjs.org/@typescript/typescript-linux-loong64/-/typescript-linux-loong64-7.0.2.tgz",
      "integrity": "sha512-uEHck9i8hoAzXPiYRib1O7miOnz23SxIeVl6F4LXox+qov1K35jHcEW6VHKvZI+pyvl7fZEP4MCU5LYvIq1GuQ==",
      "cpu": [
        "loong64"
      ],
      "license": "Apache-2.0",
      "optional": true,
      "os": [
        "linux"
      ],
      "engines": {
        "node": ">=16.20.0"
      }
    },
    "node_modules/@typescript/typescript-linux-mips64el": {
      "version": "7.0.2",
      "resolved": "https://registry.npmjs.org/@typescript/typescript-linux-mips64el/-/typescript-linux-mips64el-7.0.2.tgz",
      "integrity": "sha512-R4KvAMnE43W5Qeqb0Ly56O3mWMWIAgsMyz36DCaycd5nbg/9kzm0liw3JocfRqyJY0KPmzFjbswozXyW0DnIYA==",
      "cpu": [
        "mips64el"
      ],
      "license": "Apache-2.0",
      "optional": true,
      "os": [
        "linux"
      ],
      "engines": {
        "node": ">=16.20.0"
      }
    },
    "node_modules/@typescript/typescript-linux-ppc64": {
      "version": "7.0.2",
      "resolved": "https://registry.npmjs.org/@typescript/typescript-linux-ppc64/-/typescript-linux-ppc64-7.0.2.tgz",
      "integrity": "sha512-DORx5b3sd/4S7eayxm4FQv+A7CrkUIGRaHiwI8oiHTAI1fAPWhF4J0vAlkC8biAlHSVVwxMQ3tjZ2/DVbnQiiA==",
      "cpu": [
        "ppc64"
      ],
      "license": "Apache-2.0",
      "optional": true,
      "os": [
        "linux"
      ],
      "engines": {
        "node": ">=16.20.0"
      }
    },
    "node_modules/@typescript/typescript-linux-riscv64": {
      "version": "7.0.2",
      "resolved": "https://registry.npmjs.org/@typescript/typescript-linux-riscv64/-/typescript-linux-riscv64-7.0.2.tgz",
      "integrity": "sha512-wf0jqEDOjrPRnKwYRyyJDRo11KMbvMFrU+q4zqKyChODBzvlkbhNQfKvLxQCcwTpdDaXSHZTVuh0JoCrKCUMHQ==",
      "cpu": [
        "riscv64"
      ],
      "license": "Apache-2.0",
      "optional": true,
      "os": [
        "linux"
      ],
      "engines": {
        "node": ">=16.20.0"
      }
    },
    "node_modules/@typescript/typescript-linux-s390x": {
      "version": "7.0.2",
      "resolved": "https://registry.npmjs.org/@typescript/typescript-linux-s390x/-/typescript-linux-s390x-7.0.2.tgz",
      "integrity": "sha512-IkwJc3L7yhytWd/ewjyxNDfOmswCm9GWMJT/ue/dU4aZNbwZeYAetq42VyLmsmSjvoX7z74X6ZaYCtzAr0EuGw==",
      "cpu": [
        "s390x"
      ],
      "license": "Apache-2.0",
      "optional": true,
      "os": [
        "linux"
      ],
      "engines": {
        "node": ">=16.20.0"
      }
    },
    "node_modules/@typescript/typescript-linux-x64": {
      "version": "7.0.2",
      "resolved": "https://registry.npmjs.org/@typescript/typescript-linux-x64/-/typescript-linux-x64-7.0.2.tgz",
      "integrity": "sha512-EYdf2cNg7rgCWJnxCdJ+F3V39O8ihb37eHAu1LK8oAFizgTQbPOK7zHHXbPt8rX24COqODXeI3sIf0fCXG7H/A==",
      "cpu": [
        "x64"
      ],
      "license": "Apache-2.0",
      "optional": true,
      "os": [
        "linux"
      ],
      "engines": {
        "node": ">=16.20.0"
      }
    },
    "node_modules/@typescript/typescript-netbsd-arm64": {
      "version": "7.0.2",
      "resolved": "https://registry.npmjs.org/@typescript/typescript-netbsd-arm64/-/typescript-netbsd-arm64-7.0.2.tgz",
      "integrity": "sha512-+polYF4MF04aPpO5FTkHran9yUQDSXqy5GiSDKpsll5jy3l3+g9QLhpf39T+ePtefhXLOGrLl0QIjkQP6VnelA==",
      "cpu": [
        "arm64"
      ],
      "license": "Apache-2.0",
      "optional": true,
      "os": [
        "netbsd"
      ],
      "engines": {
        "node": ">=16.20.0"
      }
    },
    "node_modules/@typescript/typescript-netbsd-x64": {
      "version": "7.0.2",
      "resolved": "https://registry.npmjs.org/@typescript/typescript-netbsd-x64/-/typescript-netbsd-x64-7.0.2.tgz",
      "integrity": "sha512-8YIT0EHM/3dq10ZOVF/A7pc/YSMtbcecct4rWtexrnSCHOPcpC2KTLXfTCR6vDpnSiY12heNb1GiN/wu+T/FyA==",
      "cpu": [
        "x64"
      ],
      "license": "Apache-2.0",
      "optional": true,
      "os": [
        "netbsd"
      ],
      "engines": {
        "node": ">=16.20.0"
      }
    },
    "node_modules/@typescript/typescript-openbsd-arm64": {
      "version": "7.0.2",
      "resolved": "https://registry.npmjs.org/@typescript/typescript-openbsd-arm64/-/typescript-openbsd-arm64-7.0.2.tgz",
      "integrity": "sha512-APT8+ClYnuYm1u9+kgGXoMj2VzWzcymwh2gNSQVySHfkRDGOTVkoWLjCmOQSaO+PoqQ57B0flRp9SA+7GnnkzQ==",
      "cpu": [
        "arm64"
      ],
      "license": "Apache-2.0",
      "optional": true,
      "os": [
        "openbsd"
      ],
      "engines": {
        "node": ">=16.20.0"
      }
    },
    "node_modules/@typescript/typescript-openbsd-x64": {
      "version": "7.0.2",
      "resolved": "https://registry.npmjs.org/@typescript/typescript-openbsd-x64/-/typescript-openbsd-x64-7.0.2.tgz",
      "integrity": "sha512-yX7s+Q0Dln0Dt9tEzZsAjXXR/+ytBM7AlglaqyeMPxQszJ1JhlJdZ6jLA+IzldHtflX81em7lDao1xXu+aRRkg==",
      "cpu": [
        "x64"
      ],
      "license": "Apache-2.0",
      "optional": true,
      "os": [
        "openbsd"
      ],
      "engines": {
        "node": ">=16.20.0"
      }
    },
    "node_modules/@typescript/typescript-sunos-x64": {
      "version": "7.0.2",
      "resolved": "https://registry.npmjs.org/@typescript/typescript-sunos-x64/-/typescript-sunos-x64-7.0.2.tgz",
      "integrity": "sha512-dLJDGaLZ1D4HPQn62u1n8mBDkJREwMsAkCdkwd4Ieqw+x3TUyTsqY0YiBCtE6H6OzzgGk3iuZ3vFWRS+E8/d1g==",
      "cpu": [
        "x64"
      ],
      "license": "Apache-2.0",
      "optional": true,
      "os": [
        "sunos"
      ],
      "engines": {
        "node": ">=16.20.0"
      }
    },
    "node_modules/@typescript/typescript-win32-arm64": {
      "version": "7.0.2",
      "resolved": "https://registry.npmjs.org/@typescript/typescript-win32-arm64/-/typescript-win32-arm64-7.0.2.tgz",
      "integrity": "sha512-Gyl1Vy6OsWesLzmq+EP0Fb7b4Nid5232AvcA2SFcdYreldpNtYFFofPjnt62y9hQy7VTaZp65ICJjuAQRaVcIQ==",
      "cpu": [
        "arm64"
      ],
      "license": "Apache-2.0",
      "optional": true,
      "os": [
        "win32"
      ],
      "engines": {
        "node": ">=16.20.0"
      }
    },
    "node_modules/@typescript/typescript-win32-x64": {
      "version": "7.0.2",
      "resolved": "https://registry.npmjs.org/@typescript/typescript-win32-x64/-/typescript-win32-x64-7.0.2.tgz",
      "integrity": "sha512-0BQ3HkAHHlKLSp1qRvf3SUhGpGsDuhB/jgFw75guyqbxJqEaS0Cw/VFO8i2nHglJUzQCRtMMR/IBAKE3ETMC4g==",
      "cpu": [
        "x64"
      ],
      "license": "Apache-2.0",
      "optional": true,
      "os": [
        "win32"
      ],
      "engines": {
        "node": ">=16.20.0"
      }
    },
    "node_modules/typescript": {
      "version": "7.0.2",
      "resolved": "https://registry.npmjs.org/typescript/-/typescript-7.0.2.tgz",
      "integrity": "sha512-8FYau96o3NKOhbjKi/qNvG/W5jhzxkbdm5sj9AbZ/5T5sWqn3hJgLfGx27sRKZWTvyzCP8dLRBTf5tBTSRVUNA==",
      "license": "Apache-2.0",
      "bin": {
        "tsc": "bin/tsc"
      },
      "engines": {
        "node": ">=16.20.0"
      },
      "optionalDependencies": {
        "@typescript/typescript-aix-ppc64": "7.0.2",
        "@typescript/typescript-darwin-arm64": "7.0.2",
        "@typescript/typescript-darwin-x64": "7.0.2",
        "@typescript/typescript-freebsd-arm64": "7.0.2",
        "@typescript/typescript-freebsd-x64": "7.0.2",
        "@typescript/typescript-linux-arm": "7.0.2",
        "@typescript/typescript-linux-arm64": "7.0.2",
        "@typescript/typescript-linux-loong64": "7.0.2",
        "@typescript/typescript-linux-mips64el": "7.0.2",
        "@typescript/typescript-linux-ppc64": "7.0.2",
        "@typescript/typescript-linux-riscv64": "7.0.2",
        "@typescript/typescript-linux-s390x": "7.0.2",
        "@typescript/typescript-linux-x64": "7.0.2",
        "@typescript/typescript-netbsd-arm64": "7.0.2",
        "@typescript/typescript-netbsd-x64": "7.0.2",
        "@typescript/typescript-openbsd-arm64": "7.0.2",
        "@typescript/typescript-openbsd-x64": "7.0.2",
        "@typescript/typescript-sunos-x64": "7.0.2",
        "@typescript/typescript-win32-arm64": "7.0.2",
        "@typescript/typescript-win32-x64": "7.0.2"
      }
    }
  }
}
package.json
{
  "name": "typescript-keyword-type-node-sample",
  "version": "1.0.0",
  "description": "Construct and validate TypeScript keyword type nodes using createKeywordTypeNode",
  "type": "module",
  "main": "src/index.mjs",
  "scripts": {
    "test": "node test/contract.mjs"
  },
  "dependencies": {
    "typescript": "7.0.2"
  },
  "license": "MIT-0"
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify typescript.createKeywordTypeNode in pkg:npm/typescript@7.0.2",
  "kind": "HOW",
  "packages": [
    "pkg:npm/typescript@7.0.2"
  ],
  "symbols": [
    "typescript.createKeywordTypeNode"
  ]
}
src/index.mjs
import {
  createKeywordTypeNode,
} from 'typescript/unstable/ast/factory';

import {
  SyntaxKind,
  formatSyntaxKind,
  isKeywordTypeNode,
} from 'typescript/unstable/ast';

const KEYWORD_MAP = {
  string: SyntaxKind.StringKeyword,
  number: SyntaxKind.NumberKeyword,
  boolean: SyntaxKind.BooleanKeyword,
  void: SyntaxKind.VoidKeyword,
  any: SyntaxKind.AnyKeyword,
  unknown: SyntaxKind.UnknownKeyword,
  never: SyntaxKind.NeverKeyword,
  undefined: SyntaxKind.UndefinedKeyword,
  symbol: SyntaxKind.SymbolKeyword,
  bigint: SyntaxKind.BigIntKeyword,
  object: SyntaxKind.ObjectKeyword,
};

/**
 * Creates a KeywordTypeNode AST node from a keyword name string or SyntaxKind.
 *
 * @param {string | number} keyword - Keyword name or SyntaxKind enum value.
 * @returns {object} KeywordTypeNode AST node.
 */
export function createKeywordType(keyword) {
  if (typeof keyword === 'number') {
    return createKeywordTypeNode(keyword);
  }
  if (typeof keyword === 'string') {
    const kind = KEYWORD_MAP[keyword.toLowerCase()];
    if (kind !== undefined) {
      return createKeywordTypeNode(kind);
    }
    throw new Error(`Unknown keyword type name: ${keyword}`);
  }
  throw new TypeError('Expected keyword to be a string name or SyntaxKind number');
}

/**
 * Validates whether an AST node is a KeywordTypeNode, optionally checking expected kind.
 *
 * @param {object} node - AST node to check.
 * @param {string | number} [expectedKind] - Optional expected keyword name or SyntaxKind.
 * @returns {boolean} True if the node matches the keyword type.
 */
export function isKeywordType(node, expectedKind) {
  if (!node || typeof node !== 'object' || !isKeywordTypeNode(node)) {
    return false;
  }
  if (expectedKind === undefined) {
    return true;
  }
  const targetKind = typeof expectedKind === 'string' ? KEYWORD_MAP[expectedKind.toLowerCase()] : expectedKind;
  return node.kind === targetKind;
}

/**
 * Returns the human-readable keyword name and syntax kind of a KeywordTypeNode.
 *
 * @param {object} node - KeywordTypeNode AST node.
 * @returns {object} Metadata with kind, kindName, and keywordName.
 */
export function getKeywordTypeName(node) {
  if (!node || typeof node !== 'object' || !isKeywordTypeNode(node)) {
    throw new TypeError('Expected a KeywordTypeNode AST node');
  }
  const kindName = formatSyntaxKind(node.kind);
  let keywordName = Object.keys(KEYWORD_MAP).find((k) => KEYWORD_MAP[k] === node.kind);
  if (!keywordName) {
    keywordName = kindName.replace(/Keyword$/, '').toLowerCase();
  }
  return {
    kind: node.kind,
    kindName,
    keywordName,
  };
}

/**
 * Maps a dictionary of field names to keyword types, returning an object of field -> AST node.
 *
 * @param {Record<string, string | number>} schema - Field name to keyword type map.
 * @returns {Record<string, object>} Field name to KeywordTypeNode map.
 */
export function createPrimitiveTypeMap(schema) {
  if (!schema || typeof schema !== 'object') {
    throw new TypeError('Expected schema object');
  }
  const result = {};
  for (const [key, val] of Object.entries(schema)) {
    result[key] = createKeywordType(val);
  }
  return result;
}

export {
  createKeywordTypeNode,
  SyntaxKind,
  formatSyntaxKind,
  isKeywordTypeNode,
};
test/contract.mjs
import assert from 'node:assert/strict';
import {
  createKeywordTypeNode,
  createKeywordType,
  isKeywordType,
  getKeywordTypeName,
  createPrimitiveTypeMap,
  SyntaxKind,
  formatSyntaxKind,
  isKeywordTypeNode,
} from '../src/index.mjs';

// Contract 1: createKeywordTypeNode constructs keyword type nodes for primitive TypeScript types (string, number, boolean, void, any, unknown, never)
{
  const stringNode = createKeywordTypeNode(SyntaxKind.StringKeyword);
  assert.ok(stringNode, 'String keyword node should be defined');
  assert.strictEqual(isKeywordTypeNode(stringNode), true, 'stringNode should satisfy isKeywordTypeNode');
  assert.strictEqual(stringNode.kind, SyntaxKind.StringKeyword, 'Kind should be StringKeyword');
  assert.strictEqual(formatSyntaxKind(stringNode.kind), 'StringKeyword', 'Formatted kind name should be StringKeyword');

  const numberNode = createKeywordTypeNode(SyntaxKind.NumberKeyword);
  assert.ok(numberNode, 'Number keyword node should be defined');
  assert.strictEqual(isKeywordTypeNode(numberNode), true, 'numberNode should satisfy isKeywordTypeNode');
  assert.strictEqual(numberNode.kind, SyntaxKind.NumberKeyword, 'Kind should be NumberKeyword');

  const boolNode = createKeywordTypeNode(SyntaxKind.BooleanKeyword);
  assert.ok(boolNode, 'Boolean keyword node should be defined');
  assert.strictEqual(isKeywordTypeNode(boolNode), true, 'boolNode should satisfy isKeywordTypeNode');
  assert.strictEqual(boolNode.kind, SyntaxKind.BooleanKeyword, 'Kind should be BooleanKeyword');

  const voidNode = createKeywordTypeNode(SyntaxKind.VoidKeyword);
  assert.ok(voidNode, 'Void keyword node should be defined');
  assert.strictEqual(isKeywordTypeNode(voidNode), true, 'voidNode should satisfy isKeywordTypeNode');
  assert.strictEqual(voidNode.kind, SyntaxKind.VoidKeyword, 'Kind should be VoidKeyword');

  const anyNode = createKeywordTypeNode(SyntaxKind.AnyKeyword);
  assert.ok(anyNode, 'Any keyword node should be defined');
  assert.strictEqual(isKeywordTypeNode(anyNode), true, 'anyNode should satisfy isKeywordTypeNode');
  assert.strictEqual(anyNode.kind, SyntaxKind.AnyKeyword, 'Kind should be AnyKeyword');

  const unknownNode = createKeywordTypeNode(SyntaxKind.UnknownKeyword);
  assert.ok(unknownNode, 'Unknown keyword node should be defined');
  assert.strictEqual(isKeywordTypeNode(unknownNode), true, 'unknownNode should satisfy isKeywordTypeNode');
  assert.strictEqual(unknownNode.kind, SyntaxKind.UnknownKeyword, 'Kind should be UnknownKeyword');

  const neverNode = createKeywordTypeNode(SyntaxKind.NeverKeyword);
  assert.ok(neverNode, 'Never keyword node should be defined');
  assert.strictEqual(isKeywordTypeNode(neverNode), true, 'neverNode should satisfy isKeywordTypeNode');
  assert.strictEqual(neverNode.kind, SyntaxKind.NeverKeyword, 'Kind should be NeverKeyword');
}

// Contract 2: createKeywordType creates AST nodes for string names and SyntaxKind values using createKeywordTypeNode
{
  const primitives = ['string', 'number', 'boolean', 'void', 'any', 'unknown', 'never', 'undefined', 'symbol', 'bigint', 'object'];
  for (const prim of primitives) {
    const node = createKeywordType(prim);
    assert.ok(node, `Node for ${prim} should be defined`);
    assert.strictEqual(isKeywordTypeNode(node), true, `Node for ${prim} should satisfy isKeywordTypeNode`);
  }

  const directNode = createKeywordType(SyntaxKind.StringKeyword);
  assert.strictEqual(isKeywordTypeNode(directNode), true, 'Direct SyntaxKind node should satisfy isKeywordTypeNode');
  assert.strictEqual(directNode.kind, SyntaxKind.StringKeyword, 'Direct SyntaxKind node should have StringKeyword kind');

  const typeMap = createPrimitiveTypeMap({
    id: 'number',
    name: 'string',
    isActive: 'boolean',
  });
  assert.strictEqual(isKeywordTypeNode(typeMap.id), true, 'typeMap.id should be keyword type node');
  assert.strictEqual(typeMap.id.kind, SyntaxKind.NumberKeyword, 'typeMap.id kind should be NumberKeyword');
  assert.strictEqual(typeMap.name.kind, SyntaxKind.StringKeyword, 'typeMap.name kind should be StringKeyword');
  assert.strictEqual(typeMap.isActive.kind, SyntaxKind.BooleanKeyword, 'typeMap.isActive kind should be BooleanKeyword');

  assert.throws(() => createKeywordType('invalidType'), /Unknown keyword type name/, 'Should throw on invalid keyword name');
  assert.throws(() => createKeywordType(null), { name: 'TypeError' }, 'Should throw TypeError on null');
  assert.throws(() => createPrimitiveTypeMap(null), { name: 'TypeError' }, 'Should throw TypeError on null schema');
}

// Contract 3: isKeywordType validates AST nodes as keyword type nodes and checks expected SyntaxKind
{
  const strNode = createKeywordTypeNode(SyntaxKind.StringKeyword);
  assert.strictEqual(isKeywordType(strNode), true, 'isKeywordType without expected kind should be true');
  assert.strictEqual(isKeywordType(strNode, 'string'), true, 'isKeywordType with string kind should be true');
  assert.strictEqual(isKeywordType(strNode, SyntaxKind.StringKeyword), true, 'isKeywordType with SyntaxKind should be true');
  assert.strictEqual(isKeywordType(strNode, 'number'), false, 'isKeywordType with mismatched kind should be false');
  assert.strictEqual(isKeywordType(strNode, SyntaxKind.NumberKeyword), false, 'isKeywordType with mismatched SyntaxKind should be false');

  assert.strictEqual(isKeywordType({ kind: 1 }), false, 'Non-keyword object should return false for isKeywordType');
  assert.strictEqual(isKeywordType(null), false, 'null should return false for isKeywordType');
  assert.strictEqual(isKeywordType({}), false, 'plain object should return false for isKeywordType');
}

// Contract 4: getKeywordTypeName extracts human-readable keyword names and throws TypeError for non-keyword nodes
{
  const strNode = createKeywordTypeNode(SyntaxKind.StringKeyword);
  const strInfo = getKeywordTypeName(strNode);
  assert.strictEqual(strInfo.kind, SyntaxKind.StringKeyword, 'strInfo.kind should match StringKeyword');
  assert.strictEqual(strInfo.kindName, 'StringKeyword', 'strInfo.kindName should match StringKeyword');
  assert.strictEqual(strInfo.keywordName, 'string', 'strInfo.keywordName should match string');

  const numNode = createKeywordTypeNode(SyntaxKind.NumberKeyword);
  const numInfo = getKeywordTypeName(numNode);
  assert.strictEqual(numInfo.kind, SyntaxKind.NumberKeyword, 'numInfo.kind should match NumberKeyword');
  assert.strictEqual(numInfo.kindName, 'NumberKeyword', 'numInfo.kindName should match NumberKeyword');
  assert.strictEqual(numInfo.keywordName, 'number', 'numInfo.keywordName should match number');

  assert.throws(() => getKeywordTypeName(null), { name: 'TypeError' }, 'getKeywordTypeName should throw on null');
  assert.throws(() => getKeywordTypeName({ kind: 1 }), { name: 'TypeError' }, 'getKeywordTypeName should throw on non-keyword');
}

console.log('All pkg:npm/typescript@7.0.2 contracts passed successfully.');

Seeder de origen

anónimo