खोज…


वाक्य - विन्यास

  • त्रिकोण कोष्ठक के भीतर घोषित सामान्य प्रकार: <T>
  • जेनेरिक प्रकारों को बनाने का काम एक्सटेंडेड कीवर्ड के साथ किया जाता है: <T extends Car>

टिप्पणियों

सामान्य पैरामीटर रनटाइम पर उपलब्ध नहीं हैं, वे केवल संकलन समय के लिए हैं। इसका मतलब है कि आप ऐसा कुछ नहीं कर सकते:

class Executor<T, U> {
    public execute(executable: T): void {
        if (T instanceof Executable1) {    // Compilation error
            ...
        } else if (U instanceof Executable2){    // Compilation error
            ...
        }
    }
}

हालाँकि, वर्ग की जानकारी अभी भी संरक्षित है, इसलिए आप अभी भी एक चर के प्रकार के लिए परीक्षण कर सकते हैं क्योंकि आप हमेशा सक्षम रहे हैं:

class Executor<T, U> {
    public execute(executable: T): void {
        if (executable instanceof Executable1) {
            ...
        } else if (executable instanceof Executable2){
            ...
        } // But in this method, since there is no parameter of type `U` it is non-sensical to ask about U's "type"
    }
}

जेनेरिक इंटरफेस

एक सामान्य इंटरफ़ेस की घोषणा

interface IResult<T> {
    wasSuccessfull: boolean;
    error: T;
}

var result: IResult<string> = ....
var error: string = result.error;

कई प्रकार के मापदंडों के साथ सामान्य इंटरफ़ेस

interface IRunnable<T, U> {
    run(input: T): U;
}

var runnable: IRunnable<string, number> = ...
var input: string;
var result: number = runnable.run(input);

एक सामान्य इंटरफ़ेस लागू करना

interface IResult<T>{
    wasSuccessfull: boolean;
    error: T;

    clone(): IResult<T>;
}

इसे सामान्य वर्ग के साथ लागू करें:

class Result<T> implements IResult<T> {
    constructor(public result: boolean, public error: T) {
    }

    public clone(): IResult<T> {
        return new Result<T>(this.result, this.error);
    }
}

इसे गैर सामान्य वर्ग के साथ लागू करें:

class StringResult implements IResult<string> {
    constructor(public result: boolean, public error: string) {
    }

    public clone(): IResult<string> {
        return new StringResult(this.result, this.error);
    }
}

सामान्य वर्ग

class Result<T> {
    constructor(public wasSuccessful: boolean, public error: T) {
    }

    public clone(): Result<T> {
       ...
    }
}

let r1 = new Result(false, 'error: 42');  // Compiler infers T to string
let r2 = new Result(false, 42);           // Compiler infers T to number
let r3 = new Result<string>(true, null);  // Explicitly set T to string
let r4 = new Result<string>(true, 4);     // Compilation error because 4 is not a string

जेनरिक बाधाएं

सरल बाधा:

interface IRunnable {
    run(): void;
}

interface IRunner<T extends IRunnable> {
    runSafe(runnable: T): void;
}

अधिक जटिल बाधा:

interface IRunnble<U> {
    run(): U;
}

interface IRunner<T extends IRunnable<U>, U> {
    runSafe(runnable: T): U;
}

और भी जटिल:

interface IRunnble<V> {
    run(parameter: U): V;
}

interface IRunner<T extends IRunnable<U, V>, U, V> {
    runSafe(runnable: T, parameter: U): V;
}

इनलाइन प्रकार की कमी:

interface IRunnable<T extends { run(): void }> {
    runSafe(runnable: T): void;
}

सामान्य कार्य

इंटरफेस में:

interface IRunner {
    runSafe<T extends IRunnable>(runnable: T): void;
}

कक्षाओं में:

class Runner implements IRunner {

    public runSafe<T extends IRunnable>(runnable: T): void {
        try {
            runnable.run();
        } catch(e) {
        }
    }

}

सरल कार्य:

function runSafe<T extends IRunnable>(runnable: T): void {
    try {
        runnable.run();
    } catch(e) {
    }
}

सामान्य कक्षाओं और कार्यों का उपयोग करना:

सामान्य वर्ग उदाहरण बनाएँ:

var stringRunnable = new Runnable<string>();

सामान्य कार्य चलाएँ:

function runSafe<T extends Runnable<U>, U>(runnable: T);

// Specify the generic types:
runSafe<Runnable<string>, string>(stringRunnable);

// Let typescript figure the generic types by himself:
runSafe(stringRunnable);

बाधाओं के रूप में पैरामीटर टाइप करें

टाइपस्क्रिप्ट 1.8 के साथ एक प्रकार के पैरामीटर बाधा के लिए एक ही प्रकार के पैरामीटर सूची से प्रकार पैरामीटर को संदर्भित करना संभव हो जाता है। पहले यह एक त्रुटि थी।

 function assign<T extends U, U>(target: T, source: U): T {
    for (let id in source) {
        target[id] = source[id];
    }
    return target;
}

let x = { a: 1, b: 2, c: 3, d: 4 };
assign(x, { b: 10, d: 20 });
assign(x, { e: 0 });  // Error


Modified text is an extract of the original Stack Overflow Documentation
के तहत लाइसेंस प्राप्त है CC BY-SA 3.0
से संबद्ध नहीं है Stack Overflow