简体   繁体   English

当我在 Typescript 中传递给函数时,如何返回相同的类型?

[英]How can I return the same type as I pass into a function in Typescript?

I have a function which I'd like to be able to pass a class to and have it return the same type as that class.我有一个函数,我希望能够将一个类传递给它并让它返回与该类相同的类型。

Currently, the best way I've found is like so:目前,我发现的最好方法是这样的:

protected modClass<CLASS_TYPE>(modMe: CLASS_TYPE): CLASS_TYPE {
    console.log(modMe);
    return modMe;
}
const modded = modClass<SomeClass>(new SomeClass());

The above isn't awful, it does work, but it's not really returning the type of the class passed to the function so much as having it predefined and refusing to take any other class type.以上并不可怕,它确实有效,但它并没有真正返回传递给函数的类的类型,而是预定义了它并拒绝采用任何其他类类型。

What I'd like to be able to do (for example) is this:我希望能够做的(例如)是这样的:

const modded = modClass(new SomeClass());
console.log(typeof modded); // SomeClass;

Is there anyway to make a function which returns whatever the type of one of it's arguments is in TypeScript?无论如何,是否可以创建一个函数,该函数返回 TypeScript 中其中一个参数的类型?

Edit: I should mention, I'm not just trying to have the type be correct, but be checked.编辑:我应该提一下,我不只是想让类型正确,而是要检查。 I could have the modClass function return type any and typeof would be correct.我可以让 modClass 函数返回类型 any 并且typeof是正确的。 It just wouldn't have any real type checking.它只是没有任何真正的类型检查。

typeof is the Javascript operator when used in expressions and will return object for classes as it is supposed to do. typeof是在表达式中使用时的 Javascript 运算符,它将按照预期返回类的object

As for your function, the generic parameter will be inferred correctly by the compiler you don't have to specify it explicitly.至于您的函数,编译器将正确推断泛型参数,您无需明确指定它。 (Changed the name to conform to conventions but it should work regardless) (更改名称以符合约定,但无论如何它都应该工作)

class SomeClass{
  bar(){}
}
function modClass<T>(modMe: T): T {
    console.log(modMe);
    return modMe;
}
const modded = modClass(new SomeClass()) // modded is SomeClass

modded.bar()

Playground link 游乐场链接

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeScript:对于这个 function,如何将类型显式传递给 generics - TypeScript: How can I explicitly pass in the type to the generics for this function 如何在Typescript接口中为函数定义void的返回类型? - How can I define a return type of void for a function in a Typescript interface? 如何在 Typescript 中返回 NonNullable 类型? - How can I return a NonNullable type in Typescript? 如何在打字稿中返回新的Type? - How can I return a new Type in typescript? TypeScript:我可以使用类型泛型来返回不同的值并避免两次定义相同的函数吗? - TypeScript: Can I use type generics to return a different value and avoid defining the same function twice? 在 Typescript 中,如何指定可以返回多种类型的函数的返回类型? - In Typescript, how can I specify the return type of a function that can return multiple types? 如何在Typescript中将类型注入函数? - How can I inject a type into a function in Typescript? 我可以将 function(并获取其 ReturnType)传递给通用 TypeScript 类型吗? - Can I pass a function (and get its ReturnType) to a generic TypeScript type? Typescript:如何根据参数类型获取 function 的返回类型? - Typescript: How can I get return type of function based on argument type? Typescript:我如何最明确地定义一个可以保存 function 名称或该函数返回类型的变量? - Typescript: how do I most explicitly define a variable that can hold a function name or that function's return type?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM