Skip to content

Broken inference between index typed object and Record #14930

@AlexGalays

Description

@AlexGalays

TypeScript Version: 2.2.2

Code

interface Errors {
  [field: string]: {
    key: string
    message: string
  }
}

const errors: Errors = {}

function genericObjectFunction<K extends string, V>(obj: Record<K, V>): [K, V][] {
  return []
}

/*
 Argument of type 'Errors' is not assignable to parameter of type 'Record<string, never>'.
 Index signatures are incompatible.
 Type '{ key: string; message: string; }' is not assignable to type 'never'.
 */
genericObjectFunction(errors)

The types seem generally compatible though, as expected this is legal:

const errorRecord: Record<string, { key: string, message: string }> = errors

Expected behavior:
genericObjectFunction(errors) should compile.

Metadata

Metadata

Assignees

No one assigned

    Labels

    In DiscussionNot yet reached consensusSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions