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.

The `ModelObject<this>` type lose properties.

See original GitHub issue
type NonFunctionPropertyNames<T> = { [K in keyof T]: T[K] extends Function ? never : K }[keyof T];

type ModelObject<T extends object> = {
  [K in Exclude<NonFunctionPropertyNames<T>, 'model'>]: T[K];
};

class User {
  id!: number;
  name!: string;
  avatar!: string;

  toJson () {
    const rs: ModelObject<this> = 0 as any;

    return rs;
  }

  getName () {
    const json = this.toJson();

    return json.name; // Property 'name' does not exist on type 'ModelObject<this>'.
  }
}

type UserJson = NonFunctionPropertyNames<User>; // output type UserJson = "id" | "name" | "avatar"

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:4
  • Comments:6

github_iconTop GitHub Comments

2reactions
cerino-ligutomcommented, Nov 15, 2021

Experiencing this.

“typescript”: “4.1.5” “objection”: “2.2.14”

0reactions
koskimascommented, Sep 25, 2020

Ok. I think this is a bug introduced by typescript 4. Objection’s tests still use 3.9.something. Typescrip 4 probably changed how the this type is handled in some cases.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Issue with generic properties when type mapping
I believe the cause for this is that T is generic without any type restrictions/narrowing being applied to it, and therefore the type...
Read more >
Properties defined on ObservableObject are lost when ...
I am using Typescript to define objects that extend the kendo.data.ObservableObject type. These objects contain getter and setter properties ...
Read more >
Model Objects Properties | Advance Steel 2021
A model object is defined by the object type (e.g. Beam), model role ... It can be moved to another group using the...
Read more >
Managing model data in your app - Apple Developer
A property wrapper type that subscribes to an observable object and invalidates a view whenever the observable object changes.
Read more >
I/O Model Object Types - inmation Docs!
If all referenced items have a Bad object state then the setting of the BadStatePriority property will determine which of the referenced items...
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