How to use the tsutils.isUnionType function in tsutils

To help you get started, we’ve selected a few tsutils examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github palantir / tslint / src / rules / strictComparisonsRule.ts View on Github external
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)];
}
github palantir / tslint / src / rules / returnUndefinedRule.ts View on Github external
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])
        );
    }
github fossasia / susper.com / node_modules / tslint / lib / rules / restrictPlusOperandsRule.js View on Github external
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) {
github palantir / tslint / src / rules / restrictPlusOperandsRule.ts View on Github external
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";
}
github palantir / tslint / src / rules / noNullUndefinedUnionRule.ts View on Github external
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;
}
github fossasia / susper.com / node_modules / tslint / lib / rules / strictTypePredicatesRule.js View on Github external
function unionParts(type) {
    return tsutils_1.isUnionType(type) ? type.types : [type];
}
var templateObject_1;
github nativescript-rtl / ui / node_modules / tslint / lib / rules / strictTypePredicatesRule.js View on Github external
function unionParts(type) {
    return tsutils_1.isUnionType(type) ? type.types : [type];
}
var templateObject_1;
github JoshuaKGoldberg / TypeStat / src / mutations / collecting.ts View on Github external
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;
};
github woutervh- / typescript-is / src / transform-inline / visitor.ts View on Github external
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))