TypeScript definitions for models cause null values.
See original GitHub issueThanks you for solving the flush thing! This is a very nice thing to have.
Now I have this other problem. I would love to add types to my models to get editor suggestions and the general benefits of Typescript. I tried to add the type definitions as outlined here: https://next.vuex-orm.org/guide/model/decorators.html. When I did this I did get the type information and nice type suggestions in my ide so that works.
But as soon as I defined a type for a field (either with or without a decorator) the field becomes null when queried.
Maybe I am missing something?
Reproduction
Use this simple model:
import { Model, Uid, Str } from "pinia-orm";
export default class Todo extends Model {
static entity = "Todo";
// static fields() {
// return {
// id: this.uid(),
// text: this.string(""),
// name: this.string(""),
// };
// }
//
// id!: string;
// text!: number;
// name!: number;
@Uid()
id!: string;
@Str("Todo Text")
text!: string;
@Str("Todo Name")
name!: string;
}
You can try defining with the decorators or using the commented out fields static method and the type definitions.
Now have a component like this:
<template>
<button @click="todo.save({text:"Fix TS support", name:"TS Todo"})">Add todo</button>
<button @click="todo.flush()">Clear Todos</button>
<span>{{all_todo_text}}</span>
<template>
<script setup lang="ts">
import { computed } from "vue";
import Todo from "@/Todo";
const todo = useRepo(Todo);
const all_todo_text = computed(() => todo.all().map((t) => t.text));
</script>
Steps to reproduce the behavior
- Build the above code
- Click the add todo button
- Notice that the todo texts are null
- Comment out the decorators and comment in the static field method
- Notice that the todos are now the default given in the decorator (Even though I did give a value in
save
) - Add another todo and notice that the new todos do have the value given in the
.save
- Now if you comment in the type definitions below the field method everything goes to null again.
Expected behavior
I would expect the values of the model to not be affect (turn to null) by the type definitions.
Actual behavior
When adding type definitions to models the values of the fields turn to null and in the case of decorators the values given in .save
are ignored. With this the type definitions are not useable.
Issue Analytics
- State:
- Created a year ago
- Comments:8 (5 by maintainers)
Top GitHub Comments
But I bet it was super satisfying when they all to passed 😄
182 passed
🍕Woah 182 test what an amazing effort 🤩