How to use the ts-morph.TypeGuards.isTypeLiteralNode function in ts-morph

To help you get started, we’ve selected a few ts-morph 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 airtasker / spot / lib / src / parsers / utilities / parser-utility.ts View on Github external
export function extractObjectParameterProperties(
  parameter: ParameterDeclaration
): PropertySignature[] {
  // Request parameters are expected to be object literals
  const typeNode = parameter.getTypeNodeOrThrow();
  if (!TypeGuards.isTypeLiteralNode(typeNode)) {
    throw new Error("expected object literal parameter");
  }
  return typeNode.getProperties();
}
github airtasker / spot / lib / src / parsers / utilities / type-parser.ts View on Github external
// Type references must be parsed first to ensure internal type aliases are handled
  if (TypeGuards.isTypeReferenceNode(typeNode)) {
    return parseTypeReference(typeNode);
  } else if (TypeGuards.isNullLiteral(typeNode)) {
    return NULL;
  } else if (TypeGuards.isBooleanKeyword(typeNode)) {
    return BOOLEAN;
  } else if (TypeGuards.isStringKeyword(typeNode)) {
    return STRING;
  } else if (TypeGuards.isNumberKeyword(typeNode)) {
    return FLOAT;
  } else if (TypeGuards.isLiteralTypeNode(typeNode)) {
    return parseLiteralType(typeNode);
  } else if (TypeGuards.isArrayTypeNode(typeNode)) {
    return parseArrayType(typeNode);
  } else if (TypeGuards.isTypeLiteralNode(typeNode)) {
    return parseObjectLiteralType(typeNode);
  } else if (TypeGuards.isUnionTypeNode(typeNode)) {
    return parseUnionType(typeNode);
  } else if (TypeGuards.isIndexedAccessTypeNode(typeNode)) {
    return parseTypeNode(parseIndexedAccessType(typeNode));
  } else {
    throw new Error("unknown type");
  }
}
github airtasker / spot / lib / src / neu / parsers / type-parser.ts View on Github external
} else if (TypeGuards.isNullLiteral(typeNode)) {
    return ok(nullType());
    // TODO: discourage native boolean keyword?
  } else if (TypeGuards.isBooleanKeyword(typeNode)) {
    return ok(booleanType());
    // TODO: discourage native string keyword?
  } else if (TypeGuards.isStringKeyword(typeNode)) {
    return ok(stringType());
    // TODO: discourage native number keyword?
  } else if (TypeGuards.isNumberKeyword(typeNode)) {
    return ok(floatType());
  } else if (TypeGuards.isLiteralTypeNode(typeNode)) {
    return parseLiteralType(typeNode);
  } else if (TypeGuards.isArrayTypeNode(typeNode)) {
    return parseArrayType(typeNode, typeTable, lociTable);
  } else if (TypeGuards.isTypeLiteralNode(typeNode)) {
    return parseObjectLiteralType(typeNode, typeTable, lociTable);
  } else if (TypeGuards.isUnionTypeNode(typeNode)) {
    return parseUnionType(typeNode, typeTable, lociTable);
  } else if (TypeGuards.isIndexedAccessTypeNode(typeNode)) {
    return parseIndexedAccessType(typeNode, typeTable, lociTable);
  } else {
    throw new TypeNotAllowedError("unknown type", {
      file: typeNode.getSourceFile().getFilePath(),
      position: typeNode.getPos()
    });
  }
}
github airtasker / spot / lib / src / parsers / utilities / type-parser.ts View on Github external
function resolveIndexedAccessTypeReference(
  declaration: TypeAliasDeclaration | InterfaceDeclaration | TypeLiteralNode,
  literalChain: StringLiteral[]
): TypeNode {
  if (!literalChain[0]) {
    throw new Error("indexed access type error: no literal in chain");
  }

  const propertiesParentDeclaration = TypeGuards.isTypeLiteralNode(declaration)
    ? declaration
    : resolvePropertiesFromTypeAliasOrInterfaceDeclaration(declaration);

  const propertyType = propertiesParentDeclaration
    .getPropertyOrThrow(literalChain[0].getLiteralText())
    .getTypeNodeOrThrow();

  if (TypeGuards.isTypeLiteralNode(propertyType)) {
    return resolveIndexedAccessTypeReference(
      propertyType,
      literalChain.slice(1)
    );
  }

  if (TypeGuards.isExpression(propertyType)) {
    return propertyType;
github airtasker / spot / lib / src / parsers / utilities / type-parser.ts View on Github external
declaration: TypeAliasDeclaration | InterfaceDeclaration | TypeLiteralNode,
  literalChain: StringLiteral[]
): TypeNode {
  if (!literalChain[0]) {
    throw new Error("indexed access type error: no literal in chain");
  }

  const propertiesParentDeclaration = TypeGuards.isTypeLiteralNode(declaration)
    ? declaration
    : resolvePropertiesFromTypeAliasOrInterfaceDeclaration(declaration);

  const propertyType = propertiesParentDeclaration
    .getPropertyOrThrow(literalChain[0].getLiteralText())
    .getTypeNodeOrThrow();

  if (TypeGuards.isTypeLiteralNode(propertyType)) {
    return resolveIndexedAccessTypeReference(
      propertyType,
      literalChain.slice(1)
    );
  }

  if (TypeGuards.isExpression(propertyType)) {
    return propertyType;
  }

  if (!TypeGuards.isTypeReferenceNode(propertyType)) {
    throw new Error("indexed access type error: expected type reference node");
  }

  return resolveIndexedAccessTypeReference(
    getTargetDeclarationFromTypeReference(propertyType),
github airtasker / spot / lib / src / neu / parsers / parser-helpers.ts View on Github external
export function getParameterTypeAsTypeLiteralOrThrow(
  parameter: ParameterDeclaration
): TypeLiteralNode {
  const typeNode = parameter.getTypeNodeOrThrow();
  if (!TypeGuards.isTypeLiteralNode(typeNode)) {
    throw new Error("expected parameter value to be an type literal object");
  }
  return typeNode;
}