简体   繁体   English

如何使用Typescript在React中导入函数?

[英]How to import function in React with Typescript?

I'm trying to import a function in a class, and the function is located in an other file. 我正在尝试在一个类中导入一个函数,并且该函数位于另一个文件中。 I have types.ts with : 我有type.ts与:

export castToString = () => {//implementation}

And in my file form.tsx, I want to import this function : 在我的文件form.tsx中,我想导入此函数:

import {castToString } from '../types.ts'

To use this function, I call it like this : 要使用此功能,我这样称呼它:

castToString ()

But it doesn't work, an idea ? 但这是行不通的,一个主意吗? Thank you 谢谢

Remove .ts from the import statement (you will be importing the resulting JS not the TS and you should not specify the extension in either case). 从import语句中删除.ts(您将要导入生成的JS而不是TS,并且无论哪种情况都不应指定扩展名)。 Also the declaration should be export const castToString = ... 另外,声明应为export const castToString = ...

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM