我正在尝试为Ionic应用程序构建可反序列化的模型。但是,我在WebStorm面临一些奇怪的问题。我的所有模型都实现了一个Deserializable
接口,它只包含
export interface Serializable<T> {
deserialize(input: Object): T;
}
现在我有多个这样的模型
import {Serializable} from './serializable'
export class Tag implements Serializable<Tag> {
id: number;
name: string;
deserialize(input) {
this.id = input.id;
this.name = input.name;
return this;
}
}
基本上,这适用于大多数对象。但对于某些对象,WebStorm显示出奇怪的行为。让我举个例子。请参阅描述我的问题的评论:
import {Serializable} from './serializable'
import {User} from './user'
import {Tag} from './tag'
export class Comment implements Serializable<Comment> {
id: number;
time: Date;
text: string;
author: User;
deserialize(input) : Comment {
this.id = input.id;
this.time = new Date(input.time);
this.text = input.text;
// works well
let test = new Tag().deserialize(input.test);
// shows "unresolved function or method deserialize()" ??
this.author = new User().deserialize(input.author);
// works well?
let authorsTest = input.authorsTest.map(function(authorTest){return new User().deserialize(authorTest);});
function wtf() {
// works well ???
this.author = new User().deserialize(input.author);
}
return this;
}
}
对象看起来完全一样,只是其中的其他成员。 我有一些有用的物品,有些物有效。不工作的那些,只要它们被包裹在方法中就会工作吗?
此外,当我 Ctrl +单击 User
类名时,它会跳转到正确的类。当悬停它时,它还显示User
正在实现Deserializable
接口,所以不应该知道该功能吗?
编辑:这是用户类:
import {Serializable} from './serializable'
import {Location} from './location'
export class User implements Serializable<User> {
id: number;
name: string;
age: number;
location: Location;
favorited: boolean;
description: string;
images: string[];
deserialize(input) : User {
this.id = input.id;
this.name = input.name;
this.age = input.age;
this.favorited = input.favorited;
this.description = input.description;
this.images = input.images;
this.location = new Location().deserialize(input.location);
return this;
}
}