示例
ts-api-utils 2.5.0: intersectionTypeParts, isAccessExpression, isConstAssertionExpression
已验证示例 — npm ts-api-utils 2.5.0: intersectionTypeParts, isAccessExpression, isConstAssertionExpression. contract 在 node 22 · linux alpine/x64 · docker 上运行并通过.
sha256:8ff509d43350aaa289254b0d057ea24d7f23d4dbc85af1ab2fad42dfefade3ca
本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。
提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。
MIT-0
执行证据
声明的环境与签名的运行分开呈现,你可以看到这个样本究竟运行了什么、在哪里运行。
- 证据依据
- 签名契约通过
- 验证回执
- 1
- 构建过它的签名密钥
- 1
声明的环境
node 22.23 linux 24 · ubuntu · glibc 2.39 x64 node 22.23 javascript npm 10
验证运行环境
| 环境 | 契约 | 阶段 | 运行日期 |
|---|---|---|---|
| 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 |
案例
HOW- 目标
- verify pkg:npm/ts-api-utils@2.5.0
- 符号
-
- intersectionTypeParts
- isAccessExpression
- isConstAssertionExpression
- isFunctionScopeBoundary
- isIntersectionType
- isTypeFlagSet
- isUnionType
- unionTypeParts
- 环境
- node 22.23.2
- 创建时间
- 2026-08-28T02:20:04Z
契约
- isUnionType and unionTypeParts decompose union types into constituent types
- isIntersectionType and intersectionTypeParts decompose intersection types
- isTypeFlagSet inspects type flags on TypeScript types
- isConstAssertionExpression identifies as const assertion expressions
- isAccessExpression identifies property and element access expressions
- isFunctionScopeBoundary identifies function declarations as scope boundaries
文件
- PROMPT.md
- csx.json
- package-lock.json
- package.json
- spec.json
- test/contract.mjs
源代码
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 pkg:npm/ts-api-utils@2.5.0
Kind: HOW
Use EXACTLY these public packages and versions:
- pkg:npm/ts-api-utils@2.5.0
Constraints:
- executionContext: node
Required runtime conditions:
- ecosystem: npm
- language: javascript
- moduleSystem: cjs
- packageManager: npm@10.9.8
- runtime: node@22.23.2
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.
{"case":{"caseId":"case:sha256:b8f0805020f0eea8006488fa22f3fbece89080a5dcdc9569f9dfc2367b815562","constraints":{"executionContext":"node"},"contract":["isUnionType and unionTypeParts decompose union types into constituent types","isIntersectionType and intersectionTypeParts decompose intersection types","isTypeFlagSet inspects type flags on TypeScript types","isConstAssertionExpression identifies as const assertion expressions","isAccessExpression identifies property and element access expressions","isFunctionScopeBoundary identifies function declarations as scope boundaries"],"goal":"verify pkg:npm/ts-api-utils@2.5.0","kind":"HOW","packages":["pkg:npm/ts-api-utils@2.5.0"],"schemaVersion":1,"symbols":["intersectionTypeParts","isAccessExpression","isConstAssertionExpression","isFunctionScopeBoundary","isIntersectionType","isTypeFlagSet","isUnionType","unionTypeParts"]},"contractCommand":["node","test/contract.mjs"],"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/ts-api-utils@2.5.0"],"schemaVersion":1,"subject":"pkg:npm/ts-api-utils@2.5.0","symbols":["intersectionTypeParts","isAccessExpression","isConstAssertionExpression","isFunctionScopeBoundary","isIntersectionType","isTypeFlagSet","isUnionType","unionTypeParts"],"verifierAdapter":"node-typescript@1"}
{
"name": "sample-ts-api-utils",
"version": "1.0.0",
"lockfileVersion": 3,
"requires": true,
"packages": {
"": {
"name": "sample-ts-api-utils",
"version": "1.0.0",
"license": "MIT-0",
"dependencies": {
"ts-api-utils": "2.5.0",
"typescript": "5.7.3"
}
},
"node_modules/ts-api-utils": {
"version": "2.5.0",
"resolved": "https://registry.npmjs.org/ts-api-utils/-/ts-api-utils-2.5.0.tgz",
"integrity": "sha512-OJ/ibxhPlqrMM0UiNHJ/0CKQkoKF243/AEmplt3qpRgkW8VG7IfOS41h7V8TjITqdByHzrjcS/2si+y4lIh8NA==",
"license": "MIT",
"engines": {
"node": ">=18.12"
},
"peerDependencies": {
"typescript": ">=4.8.4"
}
},
"node_modules/typescript": {
"version": "5.7.3",
"resolved": "https://registry.npmjs.org/typescript/-/typescript-5.7.3.tgz",
"integrity": "sha512-84MVSjMEHP+FQRPy3pX9sTVV/INIex71s9TL2Gm5FG/WG1SqXeKyZ0k7/blY/4FdOzI12CBy1vGc4og/eus0fw==",
"license": "Apache-2.0",
"bin": {
"tsc": "bin/tsc",
"tsserver": "bin/tsserver"
},
"engines": {
"node": ">=14.17"
}
}
}
}
{
"name": "sample-ts-api-utils",
"version": "1.0.0",
"type": "module",
"license": "MIT-0",
"dependencies": {
"ts-api-utils": "2.5.0",
"typescript": "5.7.3"
}
}
{
"schemaVersion": 1,
"goal": "verify pkg:npm/ts-api-utils@2.5.0",
"kind": "HOW",
"packages": [
"pkg:npm/ts-api-utils@2.5.0"
],
"constraints": {
"executionContext": "node"
},
"runtimeConditions": {
"ecosystem": "npm",
"language": "javascript",
"moduleSystem": "cjs",
"packageManager": "npm@10.9.8",
"runtime": "node@22.23.2"
}
}
import assert from 'node:assert/strict';
import * as tsutils from 'ts-api-utils';
import ts from 'typescript';
// Setup TypeScript program and type checker for type analysis
const sourceCode = `
type StringOrNumber = string | number;
type FooAndBar = { foo: string } & { bar: number };
const constVal = [1, 2] as const;
const arrayVal = [1, 2] as number[];
const obj = { a: 1, b: 2 };
const prop = obj.a;
const elem = obj["b"];
function testFunction(x: string | number) {
if (typeof x === "string") {
return x.toUpperCase();
}
return x.toFixed(2);
}
`;
const sourceFile = ts.createSourceFile('test.ts', sourceCode, ts.ScriptTarget.Latest, true);
const defaultCompilerHost = ts.createCompilerHost({});
const customCompilerHost = {
...defaultCompilerHost,
getSourceFile: (name, languageVersion) => {
if (name === 'test.ts') return sourceFile;
return defaultCompilerHost.getSourceFile(name, languageVersion);
},
writeFile: () => {},
getDefaultLibFileName: () => defaultCompilerHost.getDefaultLibFileName({}),
useCaseSensitiveFileNames: () => true,
getCanonicalFileName: (fileName) => fileName,
getCurrentDirectory: () => '',
getNewLine: () => '\n',
fileExists: (fileName) => fileName === 'test.ts' || defaultCompilerHost.fileExists(fileName),
readFile: (fileName) => (fileName === 'test.ts' ? sourceCode : defaultCompilerHost.readFile(fileName)),
};
const program = ts.createProgram(['test.ts'], { target: ts.ScriptTarget.Latest }, customCompilerHost);
const checker = program.getTypeChecker();
// 1. isUnionType and unionTypeParts decompose union types into constituent types
const typeAliasUnion = sourceFile.statements[0];
const unionType = checker.getTypeFromTypeNode(typeAliasUnion.type);
assert.equal(tsutils.isUnionType(unionType), true, 'StringOrNumber should be identified as union type');
const unionParts = tsutils.unionTypeParts(unionType);
assert.equal(unionParts.length, 2, 'unionTypeParts should return 2 constituent types');
const unionTypeNames = unionParts.map(t => checker.typeToString(t)).sort();
assert.deepEqual(unionTypeNames, ['number', 'string'], 'union parts should be string and number');
// 2. isIntersectionType and intersectionTypeParts decompose intersection types
const typeAliasIntersection = sourceFile.statements[1];
const intersectionType = checker.getTypeFromTypeNode(typeAliasIntersection.type);
assert.equal(tsutils.isIntersectionType(intersectionType), true, 'FooAndBar should be identified as intersection type');
const intersectionParts = tsutils.intersectionTypeParts(intersectionType);
assert.equal(intersectionParts.length, 2, 'intersectionTypeParts should return 2 constituent types');
// 3. isTypeFlagSet inspects type flags on TypeScript types
assert.equal(tsutils.isTypeFlagSet(unionType, ts.TypeFlags.Union), true, 'unionType should have Union flag');
assert.equal(tsutils.isTypeFlagSet(unionParts[0], ts.TypeFlags.Union), false, 'constituent type should not have Union flag');
const stringPart = unionParts.find(t => checker.typeToString(t) === 'string');
assert.equal(tsutils.isTypeFlagSet(stringPart, ts.TypeFlags.String), true, 'string constituent should have String flag');
// 4. isConstAssertionExpression identifies as const assertion expressions
const constStmt = sourceFile.statements[2];
const constInit = constStmt.declarationList.declarations[0].initializer;
assert.equal(tsutils.isConstAssertionExpression(constInit), true, 'initializer should be identified as const assertion expression');
const arrayStmt = sourceFile.statements[3];
const arrayInit = arrayStmt.declarationList.declarations[0].initializer;
assert.equal(tsutils.isConstAssertionExpression(arrayInit), false, 'as number[] is not a const assertion expression');
// 5. isAccessExpression identifies property and element access expressions
const propStmt = sourceFile.statements[5];
const propInit = propStmt.declarationList.declarations[0].initializer;
assert.equal(tsutils.isAccessExpression(propInit), true, 'obj.a should be identified as access expression');
const elemStmt = sourceFile.statements[6];
const elemInit = elemStmt.declarationList.declarations[0].initializer;
assert.equal(tsutils.isAccessExpression(elemInit), true, 'obj["b"] should be identified as access expression');
// 6. isFunctionScopeBoundary identifies function declarations as scope boundaries
const fnStmt = sourceFile.statements[7];
assert.equal(tsutils.isFunctionScopeBoundary(fnStmt), true, 'function declaration should be a function scope boundary');
assert.equal(tsutils.isFunctionScopeBoundary(constStmt), false, 'variable statement is not a function scope boundary');
原始种子者
匿名