簡體   English   中英

react-native - * .js導入* .ts文件

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM