• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

在TypeScript,是否有任何方法可以为函数本身键入函数返回值

用户头像
it1352
帮助1

问题说明

在过去的一周里,我一直在研究如何在TypeScript中为函数本身输入函数返回值。

For the last week, I've been researching how it could be possible to type function return values to the funtion itself in TypeScript.

对我来说有什么难度类型不是TypeScript中的第一类对象(或任何其他类型系统,不确定)。

What is hard to me is types are not the first-class object in TypeScript(or any other type systems, not sure).

从某种意义上说,我正在为自我寻找一种方法参考类型;不仅可以识别自己,还可以与其他人区别开来。

In a sense, I'm seeking a way for Self-reference types; not only identifies itself but also distinguishes from any others.

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

In fact, I've implemented such a thing in vanilaJS.

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 键入某些函数



example2: specialOperation type to a certain functions

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



exapmples and test



exapmples and test

//--- 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中这样做,混合我自己的另一个实现应该是多余的。

Although I think this method is quite useful in JavaScript, and the code runs also in TypeScript, I wonder if it's possible to implement in a sophisticated TypeScript way, because if there is better and "native manner" to do this in TypeScript, mixing another implementation by my own should be quite redundant.

谢谢。

正确答案

#1

这可以通过条件类型

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

如果您对打字稿团队考虑的设计方案感到好奇,#6606 提供了一个很好的概述。

In case you are curious about the design alternatives the typescript team considered, the discussion in #6606 provides a good overview.

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /reply/detail/tanhcgabea
系列文章
更多 icon
同类精品
更多 icon
继续加载