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

Object.values can't infer type for interface #45273

Closed
marcelgerber opened this issue Aug 2, 2021 · 3 comments
Closed

Object.values can't infer type for interface #45273

marcelgerber opened this issue Aug 2, 2021 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@marcelgerber
Copy link

Bug Report

🔎 Search Terms

Object.values, any, interface

🕗 Version & Regression Information

This is the behavior in every version I tried, and I reviewed the FAQ for entries about Object.values.
In particular, this happens in version 4.3.5.

⏯ Playground Link

Playground link with relevant code

💻 Code

// Type
interface AxisInfo {
    config: { min: number, max: number }
}

// DOESN'T WORK: Case 1: Using interface
interface AxesInfo {
    x: AxisInfo
    y: AxisInfo
}

const axes: AxesInfo = {
    x: {
        config: { min: 0, max: 50 },
    },
    y: {
        config: { min: 0, max: 100 },
    },
}

// => config.max is of `any` type
Object.values(axes).forEach(({ config }) => alert(config.max))

// WORKS FINE: Case 2: Using union Record
type AxesInfo2 = Record<"x" | "y", AxisInfo>

const axes2: AxesInfo2 = {
    x: {
        config: { min: 0, max: 50 },
    },
    y: {
        config: { min: 0, max: 100 },
    },
}

// => config.max is of type `number`
Object.values(axes2).forEach(({ config }) => alert(config.max))

🙁 Actual behavior

Object.values(axes) is of type any[].
Object.values(axes2) is of type AxisInfo[], as expected.

🙂 Expected behavior

Object.values(axes) is of type AxisInfo[].

@marcelgerber marcelgerber changed the title Object.values can't infer interface type Object.values can't infer type for interface Aug 2, 2021
@marcelgerber
Copy link
Author

Note that this issue can be worked around with the rather cumbersome

export const values = <Obj>(obj: Obj): Obj[keyof Obj][] =>
    Object.values(obj)

@MartinJohns
Copy link
Contributor

See #38520.

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Aug 2, 2021
@typescript-bot
Copy link
Collaborator

This issue has been marked as a 'Duplicate' and has seen no recent activity. It has been automatically closed for house-keeping purposes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants