Skip to content

Inconsistent implicit any when referencing destructured variable more than once #32278

@codynova

Description

@codynova

TypeScript Version: 3.5.2 and 3.5.1

Search Terms: TS7022, "missing implicit any", "inconsistent implicit any", "does not have a type annotation", "referenced directly or indirectly in its own initializer"

Code

type Updater<T> = (t: T) => void;

function createUpdater<T>(t: T): [T, Updater<T>] {
    return [t, function(newT: T) {}]
}

interface State {
    value: number;
    increment: (delta: number) => void;
    decrement: (delta: number) => void;
}

const [state1, setState1] = createUpdater<State>({
    value: 2,
    increment: delta => setState1({
        ...state1,
        value: state1.value + delta
    }),
    decrement: delta => setState1({
        ...state1,
        value: state1.value - delta
    })
})

const [state2, setState2]: [State, Updater<State>] = createUpdater<State>({
    value: 2,
    increment: delta => setState2({
        ...state2,
        value: state2.value + delta
    }),
    decrement: delta => setState2({
        ...state2,
        value: state2.value - delta
    })
})

const [state3, setState3] = createUpdater<State>({
    value: 2,
    increment: delta => setState3({
        ...state3,
        value: state3.value + delta
    }),
    decrement: delta => {}
})

Expected behavior: setState1 would not trigger an error, or setState3 would trigger an error.

Actual behavior: setState1 triggers an error TS7022, while setState3 does not.

Playground Link: TypeScript playground link

Related Issues: #6935 (?)

Note: The only discernable difference in setState1 and setState3, is that setState1 is called in state1.decrement while setState3 is not called in state3.decrement. However, I'm still learning a lot about TS, and this issue seems like multiple things are at play - so I'm really not confident in my description of the problem (issue title) or my search/comparison of other issues.

Metadata

Metadata

Assignees

No one assigned

    Labels

    BugA bug in TypeScript

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions