Reputation: 61
When I compile this file I get the following error:
44:16 - error TS2693: 'Promise' only refers to a type, but is being used as a value here.
fighter.ts:44:14 - error TS2693: 'Promise' only refers to a type, but is being used as a value here.
44 return new Promise((resolve) => {
How can I describe the Promise
correctly in this case?
export interface IFighter {
_name: string;
_health: number;
_power: number;
health: () => number;
name: () => string;
setDamage: (damage: number) => void;
hit: (enemy: Fighter, point: number) => void;
knockout: () => Promise<Promise<any>>;
}
export class Fighter implements IFighter {
_name: string;
_health: number;
_power: number;
constructor(name: string, health: number, power: number) {
this._name = name;
this._health = health;
this._power = power;
}
health(): number {
return this._health;
}
name(): string {
return this._name;
}
setDamage(damage: number): void {
this._health = this._health - damage;
console.log(`${this._name} got ${damage} dmg. ${this._health}hp less`);
}
hit(enemy: Fighter, point: number): void {
let damage: number = point * this._power;
enemy.setDamage(damage);
}
knockout(): Promise<any> {
return new Promise((resolve) => {
console.log("time is over");
setTimeout(() => {
resolve((): void => {});
}, 500);
});
}
}
Upvotes: 0
Views: 152
Reputation: 249476
The error you are specifically getting is probably due to the fact that you are targeting es5
. es5
does not have promises built-in. There is a promise type, but not a promise constructor defined in the es5
typescript lib. If your runtime has the Promise
constructor, or you have a polyfill for it, you can add a lib
attribute to your tsconfig.json
to tell typescript to add the definitions for promises (the full es2015
conforming promise).
{
....
"lib": ["es5","es2015.promise","dom", "scripthost"]
....
}
Edit As @Oleksii points out knockout: () => Promise<Promise<any>>;
should be knockout: () => Promise<any>
but that is not specially related to the error in the question.;
Upvotes: 2
Reputation: 1643
change you in your IFighter
interface:
this one knockout: () => Promise<Promise<any>>;
to knockout: () => Promise<any>;
It makes no sense to write Promise<Promise<Promise<...<T>>>>
because anyway it will be represented as Promise<T>
Upvotes: 2