Files
NapCatQQ/.pnpm-store/v3/files/31/1a58278d75e4cc31da99352787ce9f0297dba30a1d3b0cb4dfeac5782604a9f2bca17658ada8eb38394e895c675293706173c9d60116b6f2284d4dc2aa7af4
T
TanTan e311ca3275
Build NapCat Artifacts / Type Check & Test (push) Waiting to run
docs: 仓库链接指向本镜像仓库
2026-11-13 09:25:58 +08:00

33 lines
1.3 KiB
Plaintext

import { TemplateLiteralGenerate } from '../template-literal/index.mjs';
// ------------------------------------------------------------------
// TypeGuard
// ------------------------------------------------------------------
import { IsTemplateLiteral, IsUnion, IsLiteral, IsNumber, IsInteger } from '../guard/kind.mjs';
// prettier-ignore
function FromTemplateLiteral(templateLiteral) {
const keys = TemplateLiteralGenerate(templateLiteral);
return keys.map(key => key.toString());
}
// prettier-ignore
function FromUnion(types) {
const result = [];
for (const type of types)
result.push(...IndexPropertyKeys(type));
return result;
}
// prettier-ignore
function FromLiteral(literalValue) {
return ([literalValue.toString()] // TS 5.4 observes TLiteralValue as not having a toString()
);
}
/** Returns a tuple of PropertyKeys derived from the given TSchema */
// prettier-ignore
export function IndexPropertyKeys(type) {
return [...new Set((IsTemplateLiteral(type) ? FromTemplateLiteral(type) :
IsUnion(type) ? FromUnion(type.anyOf) :
IsLiteral(type) ? FromLiteral(type.const) :
IsNumber(type) ? ['[number]'] :
IsInteger(type) ? ['[number]'] :
[]))];
}