Skip to content

Entries cannot be reduced to an interface due to any being assigned to never #33651

Closed
@ibarsi

Description

@ibarsi

TypeScript Version: 3.7.0-dev.20190928

Search Terms:

  • any assigned to never
  • reduce any to never
  • cannot reduce to interface

Code

Ever since my team has updated to v3.6.3, the introduction of never has been causing us headaches whenever we try to reduce an array into a single typed object. The following example illustrates the issue we're faced with.

interface Thing {
    foo: number,
    bar: string,
}

const list: Array<[keyof Thing, any]> = [['foo', 1], ['bar', 'baz']];

const result: Thing = list.reduce((obj: Thing, [key, value]) => {
    // Compile-time error is thrown here because `any` is being assigned to `never`.
    obj[key] = value;
    return obj;
}, { foo: 0, bar: ''});

My best guess is that this behaviour is due to recent changes in TS 3.5 to fix unsound writes to indexed access types. While understandable, I'm having some difficulty trying to reason about how we might achieve the behaviour demonstrated above without type aliasing obj as any or something similar? My gut tells me this should be achievable without breaking out of the type system (i.e. aliasing), which leads me to believe this could potentially be a bug.

Expected behavior:
value of type any is allowed to be set as a property of Thing.

Actual behavior:
The following compile-time error is thrown.

Playground Link:

https://www.typescriptlang.org/play/?target=99#code/JYOwLgpgTgZghgYwgAgCoAtQHNkG8BQyRyMA9qQFzIgCuAtgEbQA0hxDcUVAzmFNqwC++fAlIheyADbBeVAIJQocAJ4AeANoBrCCtIw0mEFmbI4IFQF0AfMgC8yDRoDkZUs9MBGS6ZccoHsjOHABezpaWANwiYhJgyFAQ3DRSYFQY2PbSsmAAdIkAJjRIABQlpAwAVulGJo46KqYAbnBSNBCWAJT2tgTEyBWV2rqWWS1tENH9iWA0UCADVdGCprgk5FQADKb+VM7Ogp2RQA

Related Issues:
N/A

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions