簡體   English   中英

在TypeScript中,是否有任何方法可以將函數返回值鍵入函數本身?

[英]In TypeScript, is there any method to type function return values to the function itself?

在上周,我一直在研究如何在TypeScript中將函數返回值鍵入函數本身。

對我來說,很難的是類型不是TypeScript(或其他任何類型的系統,不是很確定)中的一流對象。

從某種意義上說,我正在尋找一種自我參照類型的方法。 不僅可以識別自己,而且可以與其他任何人區分開。

實際上,我已經在vanilaJS中實現了這樣的事情。

示例1: Member類型為函數的返回值: Member

log("=Are you a member? ========= ");
const Member = a => Type(Member)([a]); // Member = a=>[a]

const alice = ["Alice"];
const bob = Member("Bob"); //["Bob"]

log(
    isType(Member)(alice)
);//false
log(
    isType(Member)(bob)
);//true

example2:對特定功能的specialOperation類型

log("=Is this a special operation? ========= ");
const specialOperation = f => Type(specialOperation)(f);

const f1 = a => a + 1; //vanilla function
const f2 = Type(specialOperation) //typed function
    (a => {
        //This function might be considered to be "special" 
        //because it does some featured operations in a context.
        return a * 2;
    });

log(
    isType(specialOperation)(f1)
);//false
log(
    isType(specialOperation)(f2)
);//true
log(
    f2(1) // f2 = a => a * 2
);//2  // just in case, let you know

實例和測試

 //--- debug use const log = (m) => { console.log(m); //IO return m; }; //---- a type sysetm in vanillaJS const typedPrimitive = T => i => { const derived = Object(i); Object.setPrototypeOf(derived, Object(i)); const typeProperty = { enumerable: false, configurable: false, writable: false, value: true }; Object.defineProperty(derived, T, typeProperty); return derived; }; const typedObject = T => i => { const handler = { get: (target, name) => name == T//must == ? true : target[name] }; return new Proxy(i, handler); }; const typed = T => i => (i !== Object(i))//primitives ? typedPrimitive(T)(i) : typedObject(T)(i) const istype = T => i => i[T] === true; const Type = T => i => (i === T) || (i == null) ? i : typed(T)(i); const isType = T => i => (i === T) ? true : (i == null) ? false : istype(T)(i); //------------------------------------------ log("=Are you a member? ========= "); const Member = a => Type(Member)([a]); // M = a=>[a] const alice = ["Alice"]; const bob = Member("Bob"); //["Bob"] log( isType(Member)(alice) );//false log( isType(Member)(bob) );//true log("=Is this a special operation? ========= "); const specialOperation = f => Type(specialOperation)(f); const f1 = a => a + 1; //vanilla function const f2 = Type(specialOperation) //typed function (a => { //This function might be considered to be "special" //because it does some featured operations in a context. return a * 2; }); log( isType(specialOperation)(f1) );//false log( isType(specialOperation)(f2) );//true log( f2(1) // f2 = a => a * 2 );//2 // just in case, let you know log("=type test of nontyped========================="); const I = a => a; //just a dummy function log( isType(I)(I) // true ); log( isType(I)(1) // false ); log( isType(I)([]) // fakse ); log( isType(I)({}) // false ); log( isType(I)("hello") //fakse ); log( isType(I)(x => x) // false ); log( isType(I)(true) // false ); log( isType(I)(false) // false ); log("=type test of typed========================="); log( isType(I)(Type(I)(I)) // true ); log( isType(I)(Type(I)(1)) // true ); log( isType(I)(Type(I)([])) // true ); log( isType(I)(Type(I)({})) // true ); log( isType(I)(Type(I)("hello")) //true ); log( isType(I)(Type(I)(x => x)) // true ); log( isType(I)(Type(I)(true)) // true ); log( isType(I)(Type(I)(false)) // true ); log( (Type(I)(false) == false) ? "Type(I)(false) == false (as should be)" : "something is wrong" ); log( (Type(I)(false) !== false)//Object !== Primitive ? "Type(I)(false) !== false (as should be)" : "something is wrong" ); log( isType(I)(Type(I)(NaN)) //true ); log( isType(I)(Type(I)(undefined)) // false ); log( isType(I)(Type(I)(null)) // false ); log( Type(I)(1) + Type(I)(2)//3 ); log( Type(I)([1, 2, 3]) );//[1, 2, 3] 

盡管我認為該方法在JavaScript中非常有用,並且代碼也可以在TypeScript中運行,但我想知道是否可以以復雜的TypeScript方式實現,因為如果有更好的“本機”方式在TypeScript中實現,則可以混合使用由我自己執行應該是多余的。

謝謝。

這可以通過打字稿2.8中引入的條件類型來實現:

let someFunction: () => String;
let x : ReturnType<typeof someFunction>;

如果您對打字稿小組考慮的設計替代方案感到好奇,請參閱#6606中的討論。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM