-
-
Notifications
You must be signed in to change notification settings - Fork 8.6k
problem with reactive
handling objects with inner properties that are classes
#5920
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
Comments
I have noticed that the SFC Playground doesn't handle the code properly so here it is again: <script lang="ts" setup>
import { reactive } from "vue";
class TestGeneric<eX extends number> {
protected x!: number;
constructor(protected y: eX) {
this.x = y;
}
}
class TestClass {
protected x!: number;
constructor(protected y: number) {
this.x = y;
}
}
interface ITest {
x: number;
y: string | undefined;
testGeneric: TestGeneric<number> | undefined;
testClass: TestClass | undefined;
}
const testObj: ITest = {
x: 3,
y: undefined,
testGeneric: undefined,
testClass: undefined,
};
/** there is error on the `test` constant, as reactive<ITest> doesn't provide `ITest` type properly. classes `TestGeneric` and `TestClass` are "trimmed" to pure `{}` types and we gat an error like "`{}` doesn't contain `x` and `y`".
* If I remove `testGeneric` and `testClass` naturally, errors are gone.
*/
const test: ITest = reactive<ITest>(testObj);
console.log(`test: ${test}`);
</script>
<template>
</template> |
What's the status? |
I ran into the same issue today, the issue has to do with protected properties on classes used inside <script setup lang="ts">
import { reactive } from 'vue'
class Person {
constructor(public name: string){}
protected species = "HUMAN"
}
const names = [ "Jim", "Pam" ]
const people = reactive(names.map(name => new Person(name)))
function foo(people: Person[]){
for (const person of people) {
console.log(person instanceof Person) // all true
}
}
foo(people)
// ^ { name: string; }[] !== Person[]
</script>
<template>
<p v-for="person in people">{{person}} - {{person instanceof Person}}</p>
<!-- ^ both objects are instances of Person class ^ -->
</template> @LinusBorg this was never triaged/picked up by the Vue team, it seems. |
I can add: I think this is because So it might not be something that we can get around, unless the type can be made without the usage of the Edit: I see now that there are other issues pointing this out. |
Link to minimal reproduction
https://sfc.vuejs.org/#eyJBcHAudnVlIjoiPHNjcmlwdCAgbGFuZz1cInRzXCIgc2V0dXA+XG5cbiAgaW1wb3J0IHsgc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HhgIGFuZCBgeWBcIi5cbiAgICogSWYgSSByZW1vdmUgYHRlc3RHZW5lcmljYCBhbmQgYHRlc3RDbGFzc2AgbmF0dXJhbGx5LCBlcnJvcnMgYXJlIGdvbmUuXG4gICAqL1xuICBjb25zdCB0ZXN0OiBJVGVzdCA9IHJlYWN0aXZlPElUZXN0Pih0ZXN0T2JqKTtcbiAgY29uc29sZS5sb2coYHRlc3Q6ICR7dGVzdH1gKTtcblxuPC9zY3JpcHQ+XG5cbjx0ZW1wbGF0ZT5cbjwvdGVtcGxhdGU+IiwiaW1wb3J0LW1hcC5qc29uIjoie1xuICBcImltcG9ydHNcIjoge1xuICAgIFwidnVlXCI6IFwiaHR0cHM6Ly91bnBrZy5jb20vQHZ1ZS9ydW50aW1lLWRvbUAzLjIuMzMvZGlzdC9ydW50aW1lLWRvbS5lc20tYnJvd3Nlci5qc1wiXG4gIH1cbn0ifQ==
Steps to reproduce
It is self-contained in the provided the SFC Playground reproduction
What is expected?
It is expected that
const test: ITest = reactive<ITest>(testObj);
works properly in regard to type matching.
What is actually happening?
I get an error from the TypeScript language server in VSC.
TypeScript types matching with
reactive
are not handling properly classes. It reduces them to empty objects{}
System Info
Any additional comments?
If this is a technical limitation and we have to do some workarounds it should be mentioned in reactive doc but there is no mention.
I can imagine we should use interfaces instead of classes but again, it would be a serious drawback for various use-cases.
The text was updated successfully, but these errors were encountered: