打字稿TSX和通用参数 [英] Typescript TSX and generic parameters

查看:101
本文介绍了打字稿TSX和通用参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Typescript引入了对JSX语法的支持.因此,我有一个表达式可以与传统的* .ts文件很好地配合,而与* .tsx的文件则不能:

Typescript introduces support for the JSX syntax. So I have an expression that works quite well with traditional *.ts files but no with *.tsx ones:

const f = <T1>(arg1: T1) => <T2>(arg2: T2) => {
   return { arg1, arg2 };
}

我想知道是否有一种方法可以使它在* .tsx文件中工作?

I wonder is there a way to make it work inside a *.tsx file?

推荐答案

您可以改用函数表达式:

You could use function expressions instead:

const f = function<T1>(arg1: T1) {
    return function<T2>(arg2: T2) {
        return { arg1, arg2 };
    };
};

或者,我发现这可行:

const f = <T1, T2>(arg1: T1) => (arg2: T2) => {
   return { arg1, arg2 };
};


在旁注中,当提供多个通用参数而不是一个通用参数时,它似乎可以很好地编译.例如,提供一个虚拟的泛型参数将使这项工作有效:


On a sidenote, it seems that it will compile fine when multiple generic parameters are provided, but not one. For example, providing a dummy generic parameter will make this work:

const f = <T1, _>(arg1: T1) => {
   return { arg1 };
};

// or just adding a comma
const g = <T1,>(arg1: T1) => {
   return { arg1 };
};

那绝对不是理想的.可能有另一种方法可以仅使用一个通用参数来实现此目的,但是我不确定.

That's definitely not ideal though. There might be another way to make this work with just one generic parameter, but I'm not sure.

这篇关于打字稿TSX和通用参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆