这是一个简单的问题,但在我迁移到TypeScript期间,我发现在很多情况下我的代码可以采用这两个方向......哪一个可能是更好的方向:{{1 }或class.ts
?
class.ts
interface.ts
interface.ts
export class ActionSet {
constructor(private name: string) {}
get ACTION(): string {
return this.name;
}
get PENDING(): string {
return `${this.name}_PENDING`;
}
get FULFILLED(): string {
return `${this.name}_FULFILLED`;
}
get REJECTED(): string {
return `${this.name}_REJECTED`;
}
}
export function createActionSet(name: string): ActionSet {
return new ActionSet(name);
}
更直观的方式:
由于export interface IActionSet {
ACTION: string;
PENDING: string;
FULFILLED: string;
REJECTED: string;
}
export function createActionSet(name: string): IActionSet {
return {
ACTION: name,
PENDING: `${name}_PENDING`,
FULFILLED: `${name}_FULFILLED`,
REJECTED: `${name}_REJECTED`
};
}
未在许多地方使用,是否值得为它创建一个类?或者这种实施是否过度?