我正在嘗試創建一個命名空間/類/介面/型別(無論我需要什么,我都研究過它們,但似乎沒有一個能夠滿足我的需要),它宣告了一些屬性和方法型別。但也有子型別,如Character.Stats
or Character.Currency
。雖然仍然具有有效 Character
型別
我需要如何格式化/布局我的打字檔案以允許該classes.ts
檔案成為有效的打字稿?
// statement to import Character type
class Player implements Character {
constructor (
public name: string, // a property
public stats: Character.Stats, // a property with a subtype typing
public currency: Character.Currency
) {
this.name = name
this.stats = stats
this.currency = currency
}
attack(target: Player) { // parameter is typing of its own class
console.log(`Attacked ${Character.name}`)
}
}
打字稿目前甚至可以嗎?
以下是我當前的測驗代碼庫:
types/items.ts
export interface Type {
name: string,
sprite: string,
}
types/characters.ts
import * as Classes from "../classes";
export interface Stats {
health: number,
attack: number,
defence: number,
}
export interface Currency {
gold: number,
ridium: number
}
export interface Type {
name: string,
stats: Stats
currency: Currency
announce(sentence: string): void,
attack(target: Type): void,
heal(item: Classes.HealingItem): void
}
classes.ts
import * as Characters from "./types/characters";
import * as Items from "./types/items"
export class Character implements Characters.Type {
constructor(
public name: string,
public stats: Characters.Stats,
public currency: Characters.Currency
) {
this.name = name;
this.stats = stats
this.currency = currency
}
announce(sentence: string) {
console.log(`I, ${this.name}, ${sentence}`)
}
attack(target: Character): void {
this.announce(`am going to attack ${target.name}`)
}
heal(item: HealingItem) {
this.stats.health = item.healthPoints;
this.announce(`used ${item.name} to heal my hp from ${this.stats.health - item.healthPoints} to ${this.stats.health}`)
}
}
export class HealingItem implements Items.Type {
constructor(
public name: string,
public sprite: string,
public healthPoints: number
) {
this.name = name
this.sprite = sprite
this.healthPoints = healthPoints
}
}
index.ts
import * as Classes from "./classes";
const hero = new Classes.Character("Bob", // name
{ // stats
health: 100,
attack: 10,
defence: 25
},
{ // currency
gold: 50,
ridium: 0
}
)
const apple = new Classes.HealingItem("Apple", "./sprites/apple.sprite", 25);
hero.heal(apple);
hero.attack(hero);
此當前代碼一切正常,但似乎此當前布局將來會導致問題。由于Items.Type
/Characters.Type
如果這是我能得到的最接近我想要的結果,那么......
簡而言之...
我想要這樣的東西
interface B {
x: string
}
namespace A {
export type v1 = number
export type v2 = boolean
export type v3 = B
}
let p: A = {
v1: 9,
v2: true,
v3: { x: "some string" }
};
let q: A.v1 = 2;
let r: A.v2 = false;
let s: A.v3 = { x: "strung" };
這不是有效的代碼,因為let p: A
不允許將命名空間作為一種型別,但希望這能描述我想要實作的目標。
uj5u.com熱心網友回復:
如果我理解正確,您希望能夠擺脫.Type
后綴,以便您可以直接寫:
class Character implements Characters {
constructor(
public name: string,
public stats: Characters.Stats, // Type as a "member" of Characters
public currency: Characters.Currency
) {}
// Etc.
}
您很困惑,因為您看不到如何“嵌入”Stats
并Currency
鍵入Characters
.
一方面,如果它是interface
,則任何“嵌入式”型別也將被視為介面的成員。
另一方面,如果它是 a namespace
,則它本身不能直接用作型別。
您實際上可以同時實作兩者,因為namespace
可以合并到其他型別中。
有了這個,我們宣告了interface
,并使用相同的名稱將它與 a合并。namespace
export interface Characters {
name: string
stats: Characters.Stats
currency: Characters.Currency
announce(sentence: string): void
attack(target: Characters): void
heal(item: HealingItem): void
}
// Merge the namespace with the type of the same name
namespace Characters {
// Make sure the "embedded" types are explicitly exported
export interface Stats {
health: number
attack: number
defence: number
}
export interface Currency {
gold: number
ridium: number
}
}
游樂場鏈接
順便說一句,在您的建構式中,如果您的類成員已經用作具有相同名稱和可見性修飾符的建構式引數,則無需顯式分配它們,請參閱類引數屬性:
TypeScript 提供了特殊的語法,用于將建構式引數轉換為具有相同名稱和值的類屬性。這些稱為引數屬性,是通過在建構式引數前面加上可見性修飾符
public
、private
、protected
或之一來創建的readonly
。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/517347.html