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

标签 javascript typescript types functional-programming

上周,我一直在研究如何在 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

示例2: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 中执行此操作,混合我自己的另一个实现应该是相当多余的。

谢谢。

最佳答案

这可以通过 conditional types 来完成Typescript 2.8 中引入:

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

如果您对 Typescript 团队考虑的设计替代方案感到好奇,请参阅 #6606 中的讨论。提供了很好的概述。

关于javascript - 在 TypeScript 中,是否有任何方法可以将函数返回值键入函数本身?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51575722/

相关文章:

php - 使用 PHP 将文本添加到图像(gif 动画)

javascript - 无法修复 "Uncaught TypeError: Cannot read property ' 0' of undefined"

javascript - 在 XMLHttpRequest 中是否需要同时检查 readyState 和状态?

javascript - 使用 jQuery 事件拖放处理滚动条

javascript - URLSearchParams 返回空对象

angularjs - 如何使用 typescript 制作 Angular 服务?

php - php 中奇怪的 boolean 表达式求解

for循环中的Scala类型不匹配错误

javascript - 使用 Mocha 和 JavaScript 语法测试 Angular 9 (TypeScript)

c - size_t (SIZE_MAX) 的最大值是否相对于其他整数类型定义?