dipscope / typemanager.ts Goto Github PK
View Code? Open in Web Editor NEWTransform JSON strings or plain objects into JS class instances.
Home Page: https://dipscope.com/type-manager/what-issues-it-solves
License: Apache License 2.0
Transform JSON strings or plain objects into JS class instances.
Home Page: https://dipscope.com/type-manager/what-issues-it-solves
License: Apache License 2.0
Describe the bug
Out of the box, my PhpStorm (or WebStorm) IDE throws a TS1238 error on the @Type
decorator when called as an expression, and a TS1240 error ("Unable to resolve signature of property decorator when called as an expression. Argument of type undefined
is not assignable to parameter of type Object
") on the @Property
decorator.
I have been having a lot of difficulty recently understanding how PhpStorm validates syntax across our monorepo, but I get this error when my "Node Interpreter" is set to my host machine's (version 20.5.0), and it goes AWAY when I set it to use Node within this app's docker dev environment (node version 18.16.0). It comes BACK if I set it to use a different container that uses node 16.13.1. On all of the above it's set to use Typescript version 5.0.4, and I don't understand why the Node version matters at all in that case.
Either way, I do see that @Type
's signature is:
export declare function Type<TType>(typeOptions?: TypeOptions<TType>): ClassDecorator;
And indeed that accepts one optional parameter.
I am not intimately familiar enough with TypeScript to understand why the runtime will invoke it with 2 parameters, but is that a legitimate issue, and do these decorators' signatures need to be updated to handle that 2nd parameter?
Thanks!
Describe the bug
I'm attempting to eliminate a few circular dependencies and am running into an error. Given that I am matching the configuration in the documentation, I figure something is wrong.
Cannot read properties of undefined (reading 'hasOwnProperty')
To Reproduce
Steps to reproduce the behavior:
I have my user class
import Model from './model';
import { Type, Property } from '@dipscope/type-manager';
import { JsonApiResource } from '@dipscope/json-api-entity-provider';
import type { IModel } from '../interfaces';
//import type { Friendship } from '@friends/services/v2/data/models/friendship';
@Type({ alias: 'users' })
@JsonApiResource({ type: 'users' })
export class User extends Model {
type = 'users';
@Property(String)
declare email: string;
@Property(Array, [User])
declare 'issued-friends': User[];
@Property(Array, [User])
declare 'held-friends': User[];
// @Property(Array, [() => Friendship])
// declare 'issued-friendships': Friendship[];
// @Property(Array, [() => Friendship])
// declare 'held-friendships': Friendship[];
}
export default User;
export function isUser(x: IModel): x is User {
return (x as User).email ? true : false;
}
Next, I have my Friendship Class
import { User } from '@core/services/v2/data/models/user';
import { Type, Property } from '@dipscope/type-manager';
export abstract class Friendship extends Model{
@Property([String, Number])
declare userId: string | number;
@Property([String, Number])
declare friendId: string | number;
@Property(() => User)
declare user: User;
@Property(() => User)
declare friend: User;
}
export default Friendship;
Expected behavior
I'd expect the library to work as it was when I was using my old Friendship class
import { User } from '@core/services/v2/data/models/user';
import { Type, Property } from '@dipscope/type-manager';
export abstract class Friendship extends Model{
@Property([String, Number])
declare userId: string | number;
@Property([String, Number])
declare friendId: string | number;
@Property(User)
declare user: User;
@Property(User)
declare friend: User;
}
export default Friendship;
Instead, I am seeing the error in the screenshots section.
Screenshots
Desktop (please complete the following information):
Additional context
Error Log.log
Is your feature request related to a problem? Please describe.
I'm trying to add this library to a NUXT App (so that I can build a native app).
The moment I add the @type() decorator, NUXT throws a VITE compilation error:
[vite-node] [VITE_ERROR] /utils/models/core/user.ts
at /utils/models/core/user.ts] {
cause: {
statusMessage: 'Vite Error',
message: '[vite-node] [VITE_ERROR] /utils/models/core/user.ts',
stack: '[vite-node] [VITE_ERROR] /utils/models/core/user.ts\n' +
'at /utils/models/core/user.ts'
},
statusCode: 500,
fatal: false,
unhandled: false,
statusMessage: 'Vite Error',
data: undefined,
__nuxt_error: true
}
Don't quite know why, and I do have the following in my .tsconfig file
{
// https://nuxt.com/docs/guide/concepts/typescript
"extends": "./.nuxt/tsconfig.json",
// ...
"compilerOptions": {
// ...
"experimentalDecorators": true,
"emitDecoratorMetadata": true,
"useDefineForClassFields": false
}
}
Describe the solution you'd like
There will likely need to be a module for adding type-manager into NUXT.
Describe alternatives you've considered
Kindof at a roadblock here.
May switch to the non-decorator method for the meantime, but the decorator method is quite pretty.
Additional context
Add any other context or screenshots about the feature request here.
Hello! Having a strange issue with TypeManager.TS.
We have multiple frontend [Vue, if it matters] applications that all have variations on some base entities common to all aspects of our business.
In my initial implementation, I started using TypeManager.TS in only one app. But I am trying to break those classes into a base class that I put into a common/shared library, and then I extend with app-specific classes. For example:
// common_lib/entities/User.entity.ts
import { Property, Type } from '@dipscope/type-manager'
@Type()
export class UserEntity {
@Property()
id: number
...
}
// common_lib/entities/Company.entity.ts
import { Property, Type } from '@dipscope/type-manager'
@Type()
export class CompanyEntity {
@Property()
id: number
...
}
// common_lib/index.ts
import { UserEntity } from './entities/User.entity'
import { CompanyEntity } from './entities/Company.entity'
export {
...
UserEntity,
CompanyEntity,
...
}
// client_app/entities/ClientUser.entity.ts
import { UserEntity, CompanyEntity } from 'common_lib'
import { Property, Type } from '@dipscope/type-manager'
@Type()
export class ClientUserEntity extends UserEntity {
@Property()
name: string
@Property(CompanyEntity)
company: CompanyEntity
// Still should have id from UserEntity and should have a reference to a Company
}
Somehow, when I import from the common app, the UserEntity does not load/instantiate properly at all.
If I copy the base User.entity.ts into my client_app
source and reference it, everything is just fine. The fact that it was exported into my library and reimported seems to break it somehow. (When I deserialize JSON, things would be weird or non-working. Like it would see that there's a company
property referencing a class, but no data would have been deserialized to fill out that class.)
FWIW, I am using Vite as my tooling/builder, and referencing my common library by setting a linked dependency in my apps' package.json (e.g. "common_lib": "link:../common_lib"
). So Vite does, I believe do things using symlinks if that affects stuff. But seemingly the only library that has trouble with this pattern is TypeManager.TS.
Thanks!
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.