CodeSampleX

Exemple

typescript 7.0.2: createTypeParameterDeclaration

Échantillon vérifié pour npm typescript 7.0.2: createTypeParameterDeclaration. Le contrat s'est exécuté sur node 22 · linux alpine/x64 · docker et a réussi.

sha256:e9924d2c9c2fd7bfe74953c390514eb52680c4ec847103188f2f3408a8b7c858

Ce réseau offre une seule chose : un échantillon qui compile. Il l'a exécuté dans un bac à sable et conservé le reçu signé. Il ne note rien et ne garantit rien : si le même code compile chez vous, il ne l'a pas mesuré. Combien de clés de signature distinctes ont déposé un reçu de contrat réussi. Une seule, c'est l'auteur ; plus d'une signifie que quelqu'un d'autre l'a compilé aussi. Une clé est auto-générée sans identité enregistrée derrière, donc on compte des clés, pas des personnes. MIT-0

Preuves d'exécution

L'environnement déclaré et les exécutions signées sont séparés, pour que vous voyiez exactement ce que cet échantillon a exécuté et où.

Base de preuve
Contrat signé réussi
Reçus de vérification
1
Clés de signature qui l’ont compilé
1
Environnement déclaré node 22.23 linux 24 · ubuntu · glibc 2.39 x64 node 22.23 javascript npm 10

Environnements des exécutions de vérification

Environnement Contrat Étapes Exécution
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

Cas

HOW
Objectif
verify typescript.createTypeParameterDeclaration in pkg:npm/typescript@7.0.2
Paquets
Symboles
  • typescript.createTypeParameterDeclaration
Environnement
node 22.23.2
Créé
2026-08-28T07:40:37Z

Contrat

  1. typescript/unstable/ast/factory exports createTypeParameterDeclaration and updateTypeParameterDeclaration
  2. createTypeParameterDeclaration constructs a basic TypeParameterDeclaration AST node
  3. createTypeParameterDeclaration constructs constrained type parameters with extends clause and default types
  4. createTypeParameterDeclaration supports variance and const modifiers
  5. updateTypeParameterDeclaration preserves identity when unchanged and creates updated node when changed
  6. inspectTypeParameterDeclaration extracts structural metadata and rejects invalid inputs

Fichiers

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

Télécharger l’artefact source (tar.gz)

Code source

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.createTypeParameterDeclaration 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.createTypeParameterDeclaration

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:0c202ec1cc42e1394093e5cc575e50bf1baa0150b53da858a8a75480aa38885c","contract":["typescript/unstable/ast/factory exports createTypeParameterDeclaration and updateTypeParameterDeclaration","createTypeParameterDeclaration constructs a basic TypeParameterDeclaration AST node","createTypeParameterDeclaration constructs constrained type parameters with extends clause and default types","createTypeParameterDeclaration supports variance and const modifiers","updateTypeParameterDeclaration preserves identity when unchanged and creates updated node when changed","inspectTypeParameterDeclaration extracts structural metadata and rejects invalid inputs"],"goal":"verify typescript.createTypeParameterDeclaration in pkg:npm/typescript@7.0.2","kind":"HOW","packages":["pkg:npm/typescript@7.0.2"],"schemaVersion":1,"symbols":["typescript.createTypeParameterDeclaration"]},"contractCommand":["node","test/contract.js"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"npm","executionContext":"node","language":"javascript","libc":"glibc","libcVersion":"2.39","moduleSystem":"cjs","os":"linux","osVersionBucket":"24","packageManager":"npm","packageManagerVersion":"10.9.8","runtime":"node","runtimeVersion":"22.23.2","schemaVersion":1},"license":"MIT-0","packages":["pkg:npm/typescript@7.0.2"],"schemaVersion":1,"subject":"pkg:npm/typescript@7.0.2","symbols":["typescript.createTypeParameterDeclaration"],"verifierAdapter":"node-typescript@1"}
index.js
const {
  createTypeParameterDeclaration,
  updateTypeParameterDeclaration,
  createIdentifier,
  createToken,
  createTypeReferenceNode
} = require('typescript/unstable/ast/factory');

const {
  SyntaxKind,
  isTypeParameterDeclaration,
  isIdentifier,
  isTypeReferenceNode,
  isToken,
  formatSyntaxKind
} = require('typescript/unstable/ast');

/**
 * Builds a basic TypeParameterDeclaration AST node.
 *
 * @param {string | object} name - Identifier name or node.
 * @param {object} [options={}] - Optional configuration.
 * @param {object} [options.constraint] - Constraint TypeNode (e.g. extends T).
 * @param {object} [options.defaultType] - Default TypeNode (e.g. = T).
 * @param {Array<object>} [options.modifiers] - Modifier tokens (e.g. const, in, out).
 * @returns {object} TypeParameterDeclaration AST node.
 */
function buildTypeParameter(name, options = {}) {
  const nameNode = typeof name === 'string' ? createIdentifier(name) : name;
  const constraintNode = typeof options.constraint === 'string'
    ? createTypeReferenceNode(createIdentifier(options.constraint), undefined)
    : options.constraint;
  const defaultNode = typeof options.defaultType === 'string'
    ? createTypeReferenceNode(createIdentifier(options.defaultType), undefined)
    : options.defaultType;
  const modifiers = Array.isArray(options.modifiers) && options.modifiers.length > 0
    ? options.modifiers
    : undefined;

  return createTypeParameterDeclaration(
    modifiers,
    nameNode,
    constraintNode,
    undefined,
    defaultNode
  );
}

/**
 * Builds a constrained TypeParameterDeclaration AST node with extends constraint and default type.
 *
 * @param {string | object} name - Type parameter name.
 * @param {string | object} constraint - Type name or node for extends constraint.
 * @param {string | object} [defaultType] - Type name or node for default type.
 * @returns {object} TypeParameterDeclaration AST node.
 */
function buildConstrainedTypeParameter(name, constraint, defaultType) {
  return buildTypeParameter(name, { constraint, defaultType });
}

/**
 * Inspects a TypeParameterDeclaration node and extracts structural metadata.
 *
 * @param {object} node - AST node to inspect.
 * @returns {{
 *   kind: number,
 *   kindName: string,
 *   isTypeParameterDeclaration: boolean,
 *   name: string,
 *   hasConstraint: boolean,
 *   hasDefaultType: boolean,
 *   modifierKinds: Array<number>
 * }}
 */
function inspectTypeParameterDeclaration(node) {
  if (!node || typeof node !== 'object') {
    throw new TypeError('Expected an AST node object');
  }

  const isTypeParam = isTypeParameterDeclaration(node);
  if (!isTypeParam) {
    return {
      kind: node.kind,
      kindName: formatSyntaxKind ? formatSyntaxKind(node.kind) : String(node.kind),
      isTypeParameterDeclaration: false,
      name: '',
      hasConstraint: false,
      hasDefaultType: false,
      modifierKinds: []
    };
  }

  const rawName = node.name ? (node.name.text ?? node.name._data?.text ?? '') : '';
  const modifierKinds = Array.isArray(node.modifiers) ? node.modifiers.map(m => m.kind) : [];

  return {
    kind: node.kind,
    kindName: formatSyntaxKind ? formatSyntaxKind(node.kind) : 'TypeParameter',
    isTypeParameterDeclaration: true,
    name: rawName,
    hasConstraint: node.constraint !== undefined,
    hasDefaultType: node.defaultType !== undefined,
    modifierKinds
  };
}

module.exports = {
  buildTypeParameter,
  buildConstrainedTypeParameter,
  inspectTypeParameterDeclaration,
  createTypeParameterDeclaration,
  updateTypeParameterDeclaration,
  createIdentifier,
  createToken,
  createTypeReferenceNode,
  SyntaxKind,
  isTypeParameterDeclaration,
  isIdentifier,
  isTypeReferenceNode,
  isToken,
  formatSyntaxKind
};
package-lock.json
{
  "name": "sample-typescript-create-type-parameter-declaration",
  "version": "1.0.0",
  "lockfileVersion": 3,
  "requires": true,
  "packages": {
    "": {
      "name": "sample-typescript-create-type-parameter-declaration",
      "version": "1.0.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": "sample-typescript-create-type-parameter-declaration",
  "version": "1.0.0",
  "private": true,
  "description": "verify typescript.createTypeParameterDeclaration in pkg:npm/typescript@7.0.2",
  "main": "index.js",
  "scripts": {
    "test": "node test/contract.js"
  },
  "dependencies": {
    "typescript": "7.0.2"
  }
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify typescript.createTypeParameterDeclaration in pkg:npm/typescript@7.0.2",
  "kind": "HOW",
  "packages": [
    "pkg:npm/typescript@7.0.2"
  ],
  "symbols": [
    "typescript.createTypeParameterDeclaration"
  ]
}
test/contract.js
const assert = require('node:assert/strict');
const {
  buildTypeParameter,
  buildConstrainedTypeParameter,
  inspectTypeParameterDeclaration,
  createTypeParameterDeclaration,
  updateTypeParameterDeclaration,
  createIdentifier,
  createToken,
  createTypeReferenceNode,
  SyntaxKind,
  isTypeParameterDeclaration,
  isIdentifier,
  isTypeReferenceNode,
  formatSyntaxKind
} = require('../index.js');

// Contract 1: typescript/unstable/ast/factory exports createTypeParameterDeclaration and updateTypeParameterDeclaration
{
  assert.strictEqual(typeof createTypeParameterDeclaration, 'function');
  assert.strictEqual(typeof updateTypeParameterDeclaration, 'function');
  assert.strictEqual(typeof isTypeParameterDeclaration, 'function');
}

// Contract 2: createTypeParameterDeclaration constructs a basic TypeParameterDeclaration AST node
{
  const idT = createIdentifier('T');
  const tp = createTypeParameterDeclaration(undefined, idT);

  assert.ok(tp, 'TypeParameter node should be created');
  assert.strictEqual(isTypeParameterDeclaration(tp), true, 'isTypeParameterDeclaration should return true');
  assert.strictEqual(tp.kind, SyntaxKind.TypeParameter, 'Node kind should be SyntaxKind.TypeParameter');
  assert.strictEqual(formatSyntaxKind(tp.kind), 'TypeParameter', 'formatSyntaxKind should return TypeParameter');
  assert.strictEqual(tp.name.text, 'T', 'Name text should be T');
  assert.strictEqual(tp.constraint, undefined, 'Constraint should be undefined');
  assert.strictEqual(tp.defaultType, undefined, 'DefaultType should be undefined');
}

// Contract 3: createTypeParameterDeclaration constructs constrained type parameters with extends clause and default types
{
  const constraintNode = createTypeReferenceNode(createIdentifier('Comparable'), undefined);
  const defaultNode = createTypeReferenceNode(createIdentifier('string'), undefined);
  const tp = createTypeParameterDeclaration(
    undefined,
    createIdentifier('Item'),
    constraintNode,
    undefined,
    defaultNode
  );

  assert.strictEqual(isTypeParameterDeclaration(tp), true);
  assert.strictEqual(tp.name.text, 'Item');
  assert.strictEqual(isTypeReferenceNode(tp.constraint), true);
  assert.strictEqual(tp.constraint.typeName.text, 'Comparable');
  assert.strictEqual(isTypeReferenceNode(tp.defaultType), true);
  assert.strictEqual(tp.defaultType.typeName.text, 'string');
}

// Contract 4: createTypeParameterDeclaration supports variance and const modifiers
{
  const inModifier = createToken(SyntaxKind.InKeyword);
  const constModifier = createToken(SyntaxKind.ConstKeyword);
  const tp = createTypeParameterDeclaration(
    [constModifier, inModifier],
    createIdentifier('Input')
  );

  assert.strictEqual(isTypeParameterDeclaration(tp), true);
  assert.ok(Array.isArray(tp.modifiers));
  assert.strictEqual(tp.modifiers.length, 2);
  assert.strictEqual(tp.modifiers[0].kind, SyntaxKind.ConstKeyword);
  assert.strictEqual(tp.modifiers[1].kind, SyntaxKind.InKeyword);
}

// Contract 5: updateTypeParameterDeclaration preserves identity when unchanged and creates updated node when changed
{
  const idK = createIdentifier('K');
  const original = createTypeParameterDeclaration(undefined, idK);
  const identical = updateTypeParameterDeclaration(original, undefined, idK, undefined, undefined, undefined);
  assert.strictEqual(identical, original, 'update with identical properties should return original instance');

  const newName = createIdentifier('V');
  const updated = updateTypeParameterDeclaration(original, undefined, newName, undefined, undefined, undefined);
  assert.notStrictEqual(updated, original, 'update with changed properties should return new instance');
  assert.strictEqual(updated.name.text, 'V');
  assert.strictEqual(isTypeParameterDeclaration(updated), true);
}

// Contract 6: inspectTypeParameterDeclaration extracts structural metadata and rejects invalid inputs
{
  const tp = buildConstrainedTypeParameter('Entity', 'BaseEntity', 'DefaultEntity');
  const info = inspectTypeParameterDeclaration(tp);

  assert.strictEqual(info.isTypeParameterDeclaration, true);
  assert.strictEqual(info.kindName, 'TypeParameter');
  assert.strictEqual(info.name, 'Entity');
  assert.strictEqual(info.hasConstraint, true);
  assert.strictEqual(info.hasDefaultType, true);
  assert.deepStrictEqual(info.modifierKinds, []);

  assert.throws(
    () => inspectTypeParameterDeclaration(null),
    { name: 'TypeError' },
    'inspectTypeParameterDeclaration should throw TypeError on non-object'
  );
}

console.log('All typescript.createTypeParameterDeclaration contracts passed successfully.');

Seeder d'origine

anonyme