0
type TMakeEmberPostMessageParams<dataT> = {
  type: string,
  data: unknown,
  mockSource?: (type: string, data: unknown) => (Promise<dataT> | dataT),
  defaultDelay?: number,
  settingName?: string,
  requestId?: string | number,
 };

type TMakeEmberPostMessage<dataT> = (
  type: TMakeEmberPostMessageParams<dataT>['type'],
  data: TMakeEmberPostMessageParams<dataT>['data'],
  mockSource?: TMakeEmberPostMessageParams<dataT>['mockSource'],
  defaultDelay?: TMakeEmberPostMessageParams<dataT>['defaultDelay'],
  settingName?: TMakeEmberPostMessageParams<dataT>['settingName'],
  requestId?: TMakeEmberPostMessageParams<dataT>['requestId'],
) => Promise<dataT>;

Is there a way to generate the args type for TMakeEmberPostMessage from TMakeEmberPostMessageParams, without specifying each argument types? That means converting the object type into multiple function argument types.

I can't change TMakeEmberPostMessage params to a single object as it's used in different codebases.

The problem is there are a few functions that are object params, and would like to Pick couple of properties from TMakeEmberPostMessageParams

Like I would like to do,

type TGetEmberUrlParams<dataT = unknown> = {
  route?: string,
  params?: Record<string, unknown>,
  queryParams?: Record<string, unknown>,
  emberMainDomain?: boolean,
  returnUrl?: boolean,
  openInNewTab?: boolean,
} & Pick<TMakeEmberPostMessageParams<dataT>, 'type' | 'mockSource' | 'defaultDelay' | 'settingName'>;
6
  • Why is it an object type, rather than a tuple type? Commented May 1, 2024 at 7:13
  • I have other functions with object type that uses makeEmberPostMessage function which is type of TMakeEmberPostMessage, and I don't want to duplicate the object type there, so I would like to create one object type and use it in this function and other functions with objects can use it as well Commented May 1, 2024 at 7:14
  • 1
    It's possible, but it's a really really bad idea. First you convert the object type to a tuple (see here and here), then you use (...args: TheTupleType) in the function parameter list. But see the linked answers for why that's a really, really bad idea. :-) As soon as you can feasibly schedule it, I'd schedule the few hours needed to update the calls to the function so you can pass an object instead. Commented May 1, 2024 at 7:16
  • thanks, I would like to use Pick for a few properties as shown in the updated question from the tuple type, so is it better instead to duplicate the type or keep it with what I've right now? Commented May 1, 2024 at 7:19
  • 1
    In your first code block, you aren't duplicating the type, you're specifying a list of parameters whose types you get from TMakeEmberPostMessageParams. That seems just fine as it is (temporarily, until you can update the function to accept an object). Commented May 1, 2024 at 7:21

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.