-
Notifications
You must be signed in to change notification settings - Fork 342
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
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
Comments
I'm encountering the same problem, currently I do something like interface Counter {
count: number,
double: number
}
export function useCounter() {
const state = reactive({
count: 0,
double: computed(() => state.count * 2)
}) as Counter;
return state
} |
Yeah, the advantage of that approach is that we can use Supporting for classes would be neat e.g.: class Counter {
count = 0
double = computed(() => this.count * 2)
increment() {
this.count++
}
}
export function useCounter() {
const state = reactive(new Counter())
return state
} |
what do you mean you need to maintain code in 2 places? defining types / interfaces describing shape of your data seems completely acceptable. |
There seems to be no clean way to fix, as TS really can't handle the type of something self referring. |
I think it's better to use export function useCounter() {
const count = ref(0)
const double = computed(() => count.value * 2)
return {
count,
double
}
} |
Good news, TS 3.7 will support Recursive Type References. |
@vmihailenco What do you mean that "class is not supported"? |
I mean that following does not work for me: class Counter {
count = 0
double = computed(() => this.count * 2)
increment() {
this.count++
}
}
export function useCounter() {
const state = reactive(new Counter())
return state
}
function useSomeHook(state: Counter) {} // <- solved When calling |
When using TypeScript and vue-cli the following classic example does not compile with TypeScript 3.6.3:
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:After all that is a common case and it does not work as expected...
The text was updated successfully, but these errors were encountered: