Type 'T' is not assignable to type 'UnwrapRef<T>' #1512
-
Reproductionn/a Steps to reproduce the bugI'm quite new to TypeScript, so if it's not a bug explanation will be appreciated.
import { defineStore } from 'pinia'
// Define constraint for our factory function generic type
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: User) {
// See linter error when trying to assign arg value to the state
this.user = u
}
}
})
} Expected behaviorThere shouldn't be an error or I shouldn't get Actual behaviorCan't assign to a state property of generic type Additional informationDependencies versions {
"dependencies": {
"awesome-notifications": "^3.1.3",
"human-readable-numbers": "^0.9.5",
"humanize-string": "^3.0.0",
"lodash.debounce": "^4.0.8",
"make-plural": "^7.1.0",
"pinia": "^2.0.16",
"pluralize": "^8.0.0",
"sortablejs": "^1.15.0",
"vue": "^3.2.37",
"vue-router": "^4.1.2"
},
"devDependencies": {
"@rushstack/eslint-patch": "^1.1.0",
"@types/jsdom": "^16.2.14",
"@types/node": "^16.11.46",
"@vitejs/plugin-vue": "^3.0.1",
"@vue/eslint-config-prettier": "^7.0.0",
"@vue/eslint-config-typescript": "^11.0.0",
"@vue/test-utils": "^2.0.2",
"@vue/tsconfig": "^0.1.3",
"eslint": "^8.5.0",
"eslint-plugin-vue": "^9.0.0",
"jsdom": "^20.0.0",
"npm-run-all": "^4.1.5",
"prettier": "^2.5.1",
"typescript": "~4.7.4",
"vite": "^3.0.1",
"vitest": "^0.18.1"
}
} |
Beta Was this translation helpful? Give feedback.
Answered by
posva
Aug 3, 2022
Replies: 1 comment 3 replies
-
This is actually intended (See vuejs/core#1324 (comment)). You only need to set the parameter to 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 }) |
Beta Was this translation helpful? Give feedback.
3 replies
Answer selected by
posva
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is actually intended (See vuejs/core#1324 (comment)). You only need to set the parameter to
UnwrapRef<User>
: