亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何解釋以下函數的聲明

如何解釋以下函數的聲明

千巷貓影 2022-10-21 14:33:26
export declare abstract class AbstractBootstrapConsole<A extends INestApplicationContext, O extends CommonBootstrapConsoleOptions = CommonBootstrapConsoleOptions> {    protected service: ConsoleService;    protected container: A;    protected readonly options: O;    constructor(options: O);    protected useDecorators(): this;    init(): Promise<A>;    getService(): ConsoleService;    getOptions(): O;    boot(argv?: string[]):         Promise<CommandResponse>;    abstract create(): Promise<A>;}我試圖理解這個函數類聲明。它是 nestjs-console 模塊的一部分。我無法理解聲明。做什么class A<B extends C, D extends E = F>意思是?將來,我將如何自己查找?我在 Typescript 的文檔中找不到這個。更新:得到了一些非常明確的答案!謝謝你們!
查看完整描述

2 回答

?
大話西游666

TA貢獻1817條經驗 獲得超14個贊

這些被稱為泛型。


class A<B extends C, D extends E = F>

您正在定義一個名為 A 的類,它接受兩個泛型參數,稱為 B 和 D。B 必須擴展接口 C,D 必須擴展接口 E。如果沒有提供泛型參數 D,則默認為接口 F。


interface C {

    name: string;

}


interface E {

    something: number;

}


interface F extends E {

    somethingElse: boolean;

}


class A<B extends C, D extends E = F> {

    constructor(b: B, d: D) {


    }

}


interface D extends C {

    surname: string;

}


const a: A<D> = {}; // Optional generic parameter default to F, constuctor parameter d must be of type F.

https://www.typescriptlang.org/docs/handbook/generics.html


查看完整回答
反對 回復 2022-10-21
?
一只名叫tom的貓

TA貢獻1906條經驗 獲得超3個贊

讓我們把它分解成獨立的部分。

class Foo<T>

這意味著這Foo是一個接受泛型類型參數的類T,對此沒有什么特別的。創建 的實例時Foo,您需要提供一個具體的T.

class Foo<T extends U>

我們在這里添加的是對泛型類型參數的約束T。您可以將其閱讀為T must be assignable to U.

一個更實際的例子可能是

class Foo<T extends any[]>

我們在其中說T必須是一個數組。

最后一點是

class Foo<T = U>

它只是提供了一個默認類型參數,以防T在創建Foo.



查看完整回答
反對 回復 2022-10-21
  • 2 回答
  • 0 關注
  • 102 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號