question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to get enum declaration from type

See original GitHub issue

Hi, i see that i can get the enum members - const members = enumDeclaration.getMembers();

but i have the type, how can i get the type declaration to get the members?

export enum Role {
  admin = "admin",
  user = "user"
}

export class User  {
  role: Role;
}

i looping other user properties and build my ajv scheme -

export const getParamSchema = (type: Type) => {
  if (type.isEnumLiteral()) {
    schema.type = "string";
    schema.enum = [];// here i want to add the enum members..
  }
}

thanks.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
dsherretcommented, Mar 13, 2021

@yantrab now that #916 is implemented you should be able to do:

nonNullableType.getUnionTypes().map(t => t.getLiteralValueOrThrow() as string)
0reactions
fasihacommented, Dec 13, 2022

@dsherret forgive me for potentially hijacking this question, I feel like it’s very similar to my question. Happy to open a new issue if that’s better!

My question: is there a way to add enum or union members? I have the following:

import { Project, Node as TypeGuards } from 'ts-morph';
var project = new Project();
var sourceFile = project.createSourceFile(
  'file.ts',
  `const c = 'ccc';
  export type Foo = 'a'|'b'|typeof c;
  `,
);
var al = sourceFile.getTypeAliasOrThrow('Foo');
var ty = al.getType();
console.log(ty.getUnionTypes().map(t=>t.getLiteralValueOrThrow()))

and I can see the literal values of my union type Foo. Great. Now, what’s the way for me to add a new element to the union 'd' say?

(I see that object expressions have addPropertyAssignment for example, and wondering if there’s an equivalent for unions?)

Read more comments on GitHub >

github_iconTop Results From Across the Web

Handbook - Enums - TypeScript
Above, we have a numeric enum where Up is initialized with 1 . ... is simple: just access any member as a property...
Read more >
Enumeration declaration - cppreference.com
An enumeration is a distinct type whose value is restricted to a range of values (see below for details), which may include several ......
Read more >
Enum in TypeScript - TutorialsTeacher
In simple words, enums allow us to declare a set of named constants i.e. a collection of related values that can be numeric...
Read more >
Enumeration (or enum) in C - GeeksforGeeks
The keyword 'enum' is used to declare new enumeration types in C and ... All unassigned names get value as value of previous...
Read more >
How to get names of enum entries? - Stack Overflow
In JS enum is an object of [value]: name so you can get all values like that Object.keys(enum) , all names Object.values(enum) and...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found