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

Get return type of interface function #16372

Closed
pie6k opened this issue Jun 8, 2017 · 3 comments
Closed

Get return type of interface function #16372

pie6k opened this issue Jun 8, 2017 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@pie6k
Copy link

pie6k commented Jun 8, 2017

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?

@mhegazy
Copy link
Contributor

mhegazy commented Jun 8, 2017

duplicate of #6239, tracked by #6606

@mhegazy mhegazy added the Duplicate An existing issue was already created label Jun 8, 2017
@ghost
Copy link

ghost commented Jun 8, 2017

You could try it the otherway around, by typing tokens in terms of the other type:

interface S {
    weekday: Date;
    month: number;
}

type Tokens = {
    [K in keyof S]: {
        possibleValues: string[],
        filter(s: string): S[K],
    }
}

const tokens: 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
        },
    },
};

const parser = {
    parseWeekday: ({ weekday, month }: S) => {
    },
}

Not sure how we're supposed to infer the parameter types of parseWeekday without an annotation though! If the name were weekday instead of parseWeekday you could type it as {[K in keyof S]: (s: S) => void; }.

@mhegazy
Copy link
Contributor

mhegazy commented Aug 17, 2017

Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

@mhegazy mhegazy closed this as completed Aug 17, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

2 participants