question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

TypeScript definitions for models cause null values.

See original GitHub issue

Thanks 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

  1. Build the above code
  2. Click the add todo button
  3. Notice that the todo texts are null
  4. Comment out the decorators and comment in the static field method
  5. Notice that the todos are now the default given in the decorator (Even though I did give a value in save)
  6. Add another todo and notice that the new todos do have the value given in the .save
  7. 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:closed
  • Created a year ago
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
tintin10qcommented, Jul 4, 2022

But I bet it was super satisfying when they all to passed 😄

182 passed 🍕

1reaction
tintin10qcommented, Jul 4, 2022

Woah 182 test what an amazing effort 🤩

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to declare a type as nullable in TypeScript?
When using emp: Nullable<Employee> , the emp variable itself is nullable. If it is not null, it should be a valid full Employee...
Read more >
Documentation - Advanced Types
Effectively, null and undefined are valid values of every type. That means it's not possible to stop them from being assigned to any...
Read more >
TypeScript Nullable | Rules and Regulations for ...
TypeScript Nullable is a special type null that has the value null. ... if set to false, then null types are always a...
Read more >
TypeScript Types Deep Dive - Part 2: The Absence of Value
A developer could use null to denote a deliberate absence of value, whereas undefined would be the web platform telling you that something...
Read more >
patch put date field which is null · Issue #2220
It's defined in the model as: @property({ type: 'string', default: null, nullable: true, }) premium_enddate: string;. (When changing type to ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found