function getType()

in src/utils/parseJsDoc.ts [30:82]


function getType(tagType: Type | undefined | null): JsDocType | null {
  if (!tagType) {
    return null;
  }

  switch (tagType.type) {
    case 'NameExpression':
      // {a}
      return { name: tagType.name };
    case 'UnionType':
      // {a|b}
      return {
        name: 'union',
        elements: tagType.elements
          .map(element => getType(element))
          .filter(Boolean as unknown as ExcludesNullish),
      };
    case 'AllLiteral':
      // {*}
      return { name: 'mixed' };
    case 'TypeApplication':
      // {Array<string>} or {string[]}
      return {
        name: 'name' in tagType.expression ? tagType.expression.name : '',
        elements: tagType.applications
          .map(element => getType(element))
          .filter(Boolean as unknown as ExcludesNullish),
      };
    case 'ArrayType':
      // {[number, string]}
      return {
        name: 'tuple',
        elements: tagType.elements
          .map(element => getType(element))
          .filter(Boolean as unknown as ExcludesNullish),
      };
    default: {
      const typeName =
        'name' in tagType && tagType.name
          ? tagType.name
          : 'expression' in tagType &&
            tagType.expression &&
            'name' in tagType.expression
          ? tagType.expression.name
          : null;
      if (typeName) {
        return { name: typeName };
      } else {
        return null;
      }
    }
  }
}