How to use the ts-morph.TypeGuards.isTypeAliasDeclaration 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 / type-parser.ts View on Github external
function parseTypeReference(
  typeNode: TypeReferenceNode
): ReferenceType | PrimitiveType | CustomPrimitiveType {
  const declaration = getTargetDeclarationFromTypeReference(typeNode);
  const name = declaration.getName();
  if (TypeGuards.isTypeAliasDeclaration(declaration)) {
    const targetTypeNode = declaration.getTypeNodeOrThrow();
    // if the type name is one of of the internal ones ensure they have not been redefined
    if (SPOT_TYPE_ALIASES.includes(name)) {
      if (TypeGuards.isTypeReferenceNode(targetTypeNode)) {
        throw new Error(`Internal type ${name} must not be redefined`);
      } else if (declaration.getType().isString()) {
        switch (name) {
          case "String":
            return STRING;
          case "Date":
            return DATE;
          case "DateTime":
            return DATETIME;
          default:
            throw new Error(`Internal type ${name} must not be redefined`);
        }
github airtasker / spot / lib / src / parsers / utilities / parser-utility.ts View on Github external
`enums are not supported (offending type: ${targetDeclaration.getName()})`
    );
  }

  // References to enum constants (e.g SomeEnum.A) are not supported either.
  if (TypeGuards.isEnumMember(targetDeclaration)) {
    throw new Error(
      `enums are not supported (offending type: ${targetDeclaration
        .getParent()
        .getName()})`
    );
  }

  if (
    TypeGuards.isInterfaceDeclaration(targetDeclaration) ||
    TypeGuards.isTypeAliasDeclaration(targetDeclaration)
  ) {
    return targetDeclaration;
  }
  throw new Error("expected a type alias or interface declaration");
}
github airtasker / spot / lib / src / parsers / utilities / parser-utility.ts View on Github external
// }
  //
  // See https://www.typescriptlang.org/docs/handbook/interfaces.html#indexable-types for details.
  if (
    TypeGuards.isInterfaceDeclaration(targetDeclaration) &&
    targetDeclaration.getIndexSignatures().length > 0
  ) {
    throw new Error(
      `indexed types are not supported (offending type: ${targetDeclaration.getName()})`
    );
  }
  // Indexed type aliases are also not supported:
  // type SomeType = {
  //   [key: string]: Integer
  // }
  if (TypeGuards.isTypeAliasDeclaration(targetDeclaration)) {
    const typeNode = targetDeclaration.getTypeNodeOrThrow();
    if (
      TypeGuards.isTypeLiteralNode(typeNode) &&
      typeNode.getIndexSignatures().length > 0
    ) {
      throw new Error(
        `indexed types are not supported (offending type: ${targetDeclaration.getName()})`
      );
    }
  }

  // Enums are not supported:
  // enum SomeEnum { A, B, C }
  if (TypeGuards.isEnumDeclaration(targetDeclaration)) {
    throw new Error(
      `enums are not supported (offending type: ${targetDeclaration.getName()})`
github airtasker / spot / lib / src / neu / parsers / type-parser.ts View on Github external
| StringType
  | DateType
  | DateTimeType
  | FloatType
  | DoubleType
  | Int32Type
  | Int64Type,
  ParserError
> {
  const declarationResult = getTargetDeclarationFromTypeReference(typeNode);
  if (declarationResult.isErr()) return declarationResult;

  const declaration = declarationResult.unwrap();
  const name = declaration.getName();

  if (TypeGuards.isTypeAliasDeclaration(declaration)) {
    const decTypeNode = declaration.getTypeNodeOrThrow();
    // if the type name is one of of the internal ones ensure they have not been redefined
    // TODO: introduce some more type safety
    if (SPOT_TYPE_ALIASES.includes(name)) {
      if (TypeGuards.isTypeReferenceNode(decTypeNode)) {
        throw new Error(`Internal type ${name} must not be redefined`);
      } else if (declaration.getType().isString()) {
        switch (name) {
          case "String":
            return ok(stringType());
          case "Date":
            return ok(dateType());
          case "DateTime":
            return ok(dateTimeType());
          default:
            throw new Error(`Internal type ${name} must not be redefined`);
github dyatko / arkit / src / parser.ts View on Github external
if (TypeGuards.isVariableStatement(statement)) {
            try {
              const structure = statement.getStructure();

              exports.push(
                ...structure.declarations.map(declaration => declaration.name)
              );
            } catch (e) {
              warn(e);
              warn("isVariableStatement", statement.getText());
            }
          } else if (
            TypeGuards.isInterfaceDeclaration(statement) ||
            TypeGuards.isClassDeclaration(statement) ||
            TypeGuards.isEnumDeclaration(statement) ||
            TypeGuards.isTypeAliasDeclaration(statement)
          ) {
            try {
              const structure = statement.getStructure();

              if (structure.name) {
                exports.push(structure.name);
              }
            } catch (e) {
              warn(e);
              warn("isInterfaceDeclaration, ...", statement.getText());
            }
          } else if (TypeGuards.isFunctionDeclaration(statement)) {
            try {
              const structure = statement.getStructure();
              trace("EXPORT", sourceFile.getBaseName(), structure);
            } catch (e) {
github airtasker / spot / lib / src / parsers / utilities / type-parser.ts View on Github external
function resolvePropertiesFromTypeAliasOrInterfaceDeclaration(
  declaration: TypeAliasDeclaration | InterfaceDeclaration
): TypeLiteralNode | InterfaceDeclaration {
  if (TypeGuards.isInterfaceDeclaration(declaration)) {
    return declaration;
  } else if (TypeGuards.isTypeAliasDeclaration(declaration)) {
    const literalTypeNode = declaration.getTypeNodeOrThrow();

    if (TypeGuards.isTypeLiteralNode(literalTypeNode)) {
      return literalTypeNode;
    }

    throw new Error("indexed access type error: not a type literal node");
  }

  throw new Error(
    "indexed access type error: not an type alias or interface declaration"
  );
}