Skip to content

Type 'T' is not assignable to type 'UnwrapRef<T>' #1512

Closed Answered by posva
f3oall asked this question in Help and Questions
Discussion options

You must be logged in to vote

This is actually intended (See vuejs/core#1324 (comment)). You only need to set the parameter to UnwrapRef<User>:

interface Model {
  id: number
}

// Define generic factory function
export function init<User extends Model>(name = 'settings') {
  return defineStore(name, {
    state: () => {
      return {
        // Set one of the properties to the generic type
        user: {} as User
      }
    },
    actions: {
      // Add action which accepts argument with our generic type
      set(u: UnwrapRef<User>) {
        // See linter error when trying to assign arg value to the state
        this.user = u
      }
    }
  })
}

const s = init()()
s.set({ id: 1 })

Replies: 1 comment 3 replies

Comment options

You must be logged in to vote
3 replies
@f3oall
Comment options

@andredewaard
Comment options

@Mensix
Comment options

Answer selected by posva
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
4 participants
Converted from issue

This discussion was converted from issue #1510 on August 02, 2022 07:41.