如何让*.js
文件导入*.ts
的文件react-native
,但没有任何两个文件重命名?
我们想从.js导入src/lib/setGlobalStyle
.ts文件以下src/App
//MIT LICENSE from: https://github.com/Ajackster/react-native-global-props
import React from 'react'
import { StyleProp, ViewStyle } from 'react-native'
export function setGlobalStyle(obj, customProps) {
const oldRender = obj.prototype.render;
const initialDefaultProps = obj.prototype.constructor.defaultProps;
obj.prototype.constructor.defaultProps = {
...initialDefaultProps,
...customProps,
}
obj.prototype.render = function render() {
let oldProps = this.props;
this.props = { ...this.props, style: [customProps.style, this.props.style] };
try {
return oldRender.apply(this, arguments);
} finally {
this.props = oldProps;
}
};
}
但低于进口这里面App
的.js只能当我们重命名setGlobalStyle
的.ts文件setGlobalStyle
的.js:
import * as Utils from './lib/setGlobalStyle'
当然,setGlobalStyle
.ts当前不包含任何TypeScript
类型,这是因为我们必须删除所有类型并将其重命名为.js,以便我们可以继续进行项目直到得到答案。
注意:我们需要这样做的原因TypeScript
是允许IDE
参数(即customProps
参数)的自动完成。
Javascript中的JSDoc支持使您可以直接导入类型定义。我知道这与导入实际模块不同(但是如果类型定义存在,我认为这很疯狂):
/**
* @param {import("./mymodule").customProps } customProps
*/
export function setGlobalStyle(obj, customProps) {
customProps. // has full auto-completion
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句