微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Javascript-在TypeScript中,是否有任何方法可以将函数返回值键入函数本身?

在上周,我一直在研究如何在TypeScript中将函数返回值键入函数本身.

对我来说,很难的是类型不是TypeScript(或其他任何类型的系统,不是很确定)中的一流对象.

从某种意义上说,我正在寻找一种自我参照类型的方法.不仅可以识别自己,而且可以与其他任何人区分开.

实际上,我已经在vanilaJS中实现了这样的事情.

示例1:成员类型为函数的返回值:成员

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:对特定功能的特殊操作类型

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中引入的conditional types来完成:

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

如果您对打字稿小组考虑的设计替代方案感到好奇,可以在#6606中进行很好的概述.

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐