Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
function getTypes(types: ts.Type): TypeKind[] {
// Compatibility for TypeScript pre-2.4, which used EnumLiteralType instead of LiteralType
const baseType = ((types as any) as { baseType: ts.LiteralType }).baseType;
return isUnionType(types)
? Array.from(new Set(types.types.map(getKind)))
: isTypeFlagSet(types, ts.TypeFlags.EnumLiteral) && typeof baseType !== "undefined"
? [getKind(baseType)]
: [getKind(types)];
}
function isEffectivelyVoidPromise(type: ts.Type): boolean {
// Would need access to `checker.getPromisedTypeOfPromise` to do this properly.
// Assume that the return type is the global Promise (since this is an async function) and get its type argument.
if (
// tslint:disable-next-line:no-bitwise
isTypeFlagSet(type, ts.TypeFlags.Void | ts.TypeFlags.Undefined) ||
(isUnionType(type) && type.types.every(isEffectivelyVoidPromise))
) {
return true;
}
const typeArguments = getTypeArgumentsOfType(type);
return (
typeArguments !== undefined &&
typeArguments.length === 1 &&
isEffectivelyVoidPromise(typeArguments[0])
);
}
function getBaseTypeOfLiteralType(type) {
if (tsutils_1.isTypeFlagSet(type, ts.TypeFlags.StringLiteral) ||
tsutils_1.isTypeFlagSet(type, ts.TypeFlags.String)) {
return "string";
}
else if (tsutils_1.isTypeFlagSet(type, ts.TypeFlags.NumberLiteral) ||
tsutils_1.isTypeFlagSet(type, ts.TypeFlags.Number)) {
return "number";
}
else if (tsutils_1.isUnionType(type) && !tsutils_1.isTypeFlagSet(type, ts.TypeFlags.Enum)) {
var types = type.types.map(getBaseTypeOfLiteralType);
return allSame(types) ? types[0] : "invalid";
}
else if (tsutils_1.isTypeFlagSet(type, ts.TypeFlags.EnumLiteral)) {
// Compatibility for TypeScript pre-2.4, which used EnumLiteralType instead of LiteralType
getBaseTypeOfLiteralType(type.baseType);
}
return "invalid";
}
function allSame(array) {
if (
isTypeFlagSet(type, ts.TypeFlags.StringLiteral) ||
isTypeFlagSet(type, ts.TypeFlags.String)
) {
return "string";
} else if (
isTypeFlagSet(type, ts.TypeFlags.NumberLiteral) ||
isTypeFlagSet(type, ts.TypeFlags.Number)
) {
return "number";
} else if (
isTypeFlagSet(type, ts.TypeFlags.BigIntLiteral) ||
isTypeFlagSet(type, ts.TypeFlags.BigInt)
) {
return "bigint";
} else if (isUnionType(type) && !isTypeFlagSet(type, ts.TypeFlags.Enum)) {
const types = type.types.map(getBaseTypeOfLiteralType);
return allSame(types) ? types[0] : "invalid";
} else if (isTypeFlagSet(type, ts.TypeFlags.EnumLiteral)) {
// Compatibility for TypeScript pre-2.4, which used EnumLiteralType instead of LiteralType
getBaseTypeOfLiteralType(((type as any) as { baseType: ts.LiteralType }).baseType);
}
return "invalid";
}
function isNullUndefinedUnion(type: ts.Type): boolean {
if (isTypeReference(type) && type.typeArguments !== undefined) {
return type.typeArguments.some(isNullUndefinedUnion);
}
if (isUnionType(type)) {
let hasNull = false;
let hasUndefined = false;
for (const subType of type.types) {
hasNull = hasNull || subType.getFlags() === ts.TypeFlags.Null;
hasUndefined = hasUndefined || subType.getFlags() === ts.TypeFlags.Undefined;
if (hasNull && hasUndefined) {
return true;
}
}
}
return false;
}
function unionParts(type) {
return tsutils_1.isUnionType(type) ? type.types : [type];
}
var templateObject_1;
function unionParts(type) {
return tsutils_1.isUnionType(type) ? type.types : [type];
}
var templateObject_1;
export const recursivelyCollectSubTypes = (type: ts.UnionType): ts.Type[] => {
const subTypes: ts.Type[] = [];
for (const subType of type.types) {
if (tsutils.isUnionType(subType)) {
subTypes.push(...recursivelyCollectSubTypes(subType));
} else {
subTypes.push(subType);
}
}
return subTypes;
};
function visitUnionOrIntersectionType(type: ts.Type, accessor: ts.Expression, visitorContext: VisitorContext) {
let token: ts.SyntaxKind.BarBarToken | ts.SyntaxKind.AmpersandAmpersandToken;
if (tsutils.isUnionType(type)) {
if (visitorContext.mode.type === 'keyof' || visitorContext.mode.type === 'indexed-access') {
token = ts.SyntaxKind.AmpersandAmpersandToken;
} else {
token = ts.SyntaxKind.BarBarToken;
}
} else if (tsutils.isIntersectionType(type)) {
if (visitorContext.mode.type === 'keyof' || visitorContext.mode.type === 'indexed-access') {
token = ts.SyntaxKind.BarBarToken;
} else {
token = ts.SyntaxKind.AmpersandAmpersandToken;
}
} else {
throw new Error('UnionOrIntersection type is expected to be a Union or Intersection type.');
}
return type.types
.map((type) => visitType(type, accessor, visitorContext))