Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is it possible to conditionally get a property from a nested object? #687

Closed
h2ck4u opened this issue Sep 16, 2023 · 0 comments
Closed

Is it possible to conditionally get a property from a nested object? #687

h2ck4u opened this issue Sep 16, 2023 · 0 comments

Comments

@h2ck4u
Copy link

h2ck4u commented Sep 16, 2023

I'm writing in hopes of getting some help.

I would like to know if it is possible to have a scenario like this one:

interface Paths {
  '/api/pet': {
      'get' :{},
  },
  '/api/store': {
      'get' :{},
  },
  '/api/pet/{id}': {
     'post': {},
  },
  '/api/store/{id}': {
     'post': {},
  },
};

type Methods = 'get' | 'post' | 'put' | 'delete' ;


type PathKeysWithMethod<Paths, TMethod extends Methods> = SomeUtils<Paths, "Some condition here"> ;

const pathOfGetMethod: PathKeysWithMethod<'get', Paths>
// expect '/api/pet', '/api/store' 

const pathOfPostMethod: PathKeysWithMethod<'post', Paths>
// expect '/api/pet/{id}', '/api/store/{id}' 

i tried

type PathKeysWithMethod<Paths , TMethod extends Methods> = StringKeyOf<
  ConditionalPickDeep<Paths, Record<TMethod, unknown>>;

const pathOfPostMethod: PathKeysWithMethod<'post', Paths>

// result
'/api/pet', '/api/store', '/api/pet/{id}', '/api/store/{id}', 

//expected 
'/api/pet/{id}', '/api/store/{id}'
@h2ck4u h2ck4u closed this as completed Sep 16, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant