Skip to content

Typescript state auto-typing and implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer #131

@vmihailenco

Description

@vmihailenco

When using TypeScript and vue-cli the following classic example does not compile with TypeScript 3.6.3:

export function useCounter() {
  const state = reactive({
    count: 0,
    double: computed(() => state.count * 2),
  })
  return state
}

because 'state' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer.. I can "fix" it with following code but I wonder if there are better ways without resorting to manually creating a type definition for the state:

export function useCounter() {
  let self: any = null
  const state = reactive({
    count: 0,
    double: computed(() => self.count * 2),
  })
  self = state
  return state
}

After all that is a common case and it does not work as expected...

Metadata

Metadata

Assignees

No one assigned

    Labels

    cantfixThis issue can't be fixed now.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions