[英]react-native - *.js importing *.ts file
如何允许*.js
文件以react-native
方式导入*.ts
文件但不重命名任何两个文件 ?
我们想从src/App
.js导入src/lib/setGlobalStyle
.ts文件下面
//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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.