Skip to content

Get return type of interface function #16372

Closed
@pie6k

Description

@pie6k

I've got object that has some function that returns various types. I have another object that is aware of the first object, and I need to know return type of function from first object

Example:

I've got first object like:

const tokens = {
  // key below is name of token
  month: {
    possibleValues: ['jan', 'feb'], // etc...
    filter: (monthName: string): number => {
      return 12; // will return number 1-12
    }, 
  },
  weekday: {
    possibleValues: ['mon', 'tue'], // etc
    filter: (monthName: string): Date => {
      return new Date(); // will return Date of weekday
    },
  },
};

// as seen above - filter of 'month' always returns number and filter of 'weekday' returns Date

const parser: Parser<typeof tokens> {
  parseWeekday: ({ weekday, month }) => {
    // weekday should be detected to be Date type
    // month should be detected to be number type
  }
}

Is that possible?

Metadata

Metadata

Assignees

No one assigned

    Labels

    DuplicateAn existing issue was already created

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions