gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 01:25:22 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com