最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to use prop-types as type definition in typescript? - Stack Overflow

programmeradmin4浏览0评论

Situation:

Consider having the myTypes constant holding prop-types (written somewhere in a file called my-component.js), like below:

import React from 'react'
import { View } from 'react-native'
import PropTypes from 'prop-types'

export const myTypes = {
  activeColor: PropTypes.string,
  color: PropTypes.string,
  fontFamily: PropTypes.string,
  fontSize: PropTypes.number,
  fontWeight: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
  height: PropTypes.number,
  icon: PropTypes.node,
  iconOverlay: PropTypes.node,
  marginBottom: PropTypes.number,
  marginLeft: PropTypes.number,
  marginRight: PropTypes.number,
  marginTop: PropTypes.number,
  maxHeight: PropTypes.number,
  minHeight: PropTypes.number,
  onBlur: PropTypes.func,
  onChangeText: PropTypes.func,
  paddingBottom: PropTypes.number,
  paddingLeft: PropTypes.number,
  paddingRight: PropTypes.number,
  paddingTop: PropTypes.number
}

export default class MyComponent extends React.Component {
  static propTypes = myTypes
  
  render () {
    return (
      <View></View>
    );
  }
}

How would you use myTypes as a type or helper to enable IDE auto-completion?

What I tried (in another file written in type-script instead) is below:

import MyComponent, { myTypes } from 'my-component';

const dark_theme_properties: myTypes = {
  activeColor: 'green'
};

But of course, that gives the 'myTypes' refers to a value, but is being used as a type here. ts(2749) error, and using typeof myTypes is not giving the right auto-complete in IDE either.

Note that the component is written in JavaScript ES6 while the desired auto-complete is expected in type-script (where aforementioned JS is imported).

Situation:

Consider having the myTypes constant holding prop-types (written somewhere in a file called my-component.js), like below:

import React from 'react'
import { View } from 'react-native'
import PropTypes from 'prop-types'

export const myTypes = {
  activeColor: PropTypes.string,
  color: PropTypes.string,
  fontFamily: PropTypes.string,
  fontSize: PropTypes.number,
  fontWeight: PropTypes.oneOfType([PropTypes.string, PropTypes.number]),
  height: PropTypes.number,
  icon: PropTypes.node,
  iconOverlay: PropTypes.node,
  marginBottom: PropTypes.number,
  marginLeft: PropTypes.number,
  marginRight: PropTypes.number,
  marginTop: PropTypes.number,
  maxHeight: PropTypes.number,
  minHeight: PropTypes.number,
  onBlur: PropTypes.func,
  onChangeText: PropTypes.func,
  paddingBottom: PropTypes.number,
  paddingLeft: PropTypes.number,
  paddingRight: PropTypes.number,
  paddingTop: PropTypes.number
}

export default class MyComponent extends React.Component {
  static propTypes = myTypes
  
  render () {
    return (
      <View></View>
    );
  }
}

How would you use myTypes as a type or helper to enable IDE auto-completion?

What I tried (in another file written in type-script instead) is below:

import MyComponent, { myTypes } from 'my-component';

const dark_theme_properties: myTypes = {
  activeColor: 'green'
};

But of course, that gives the 'myTypes' refers to a value, but is being used as a type here. ts(2749) error, and using typeof myTypes is not giving the right auto-complete in IDE either.

Note that the component is written in JavaScript ES6 while the desired auto-complete is expected in type-script (where aforementioned JS is imported).

Share Improve this question edited Sep 24, 2020 at 7:05 CommunityBot 11 silver badge asked May 20, 2019 at 7:43 Top-MasterTop-Master 8,7455 gold badges49 silver badges86 bronze badges 1
  • I think you can use field types one by one: Props { activeColor: myTypes['activeColor'] } – Doğancan Arabacı Commented May 20, 2019 at 8:26
Add a comment  | 

3 Answers 3

Reset to default 16

We can use InferProps of @types/prop-types package to extract raw-type from prop-type objects, like:

import PropTypes, { InferProps } from 'prop-types';

const myTypes = {
  activeColor: PropTypes.string,
  // ...
};

type MyComponentProps = InferProps<typeof myTypes>;

const dark_theme_properties: MyComponentProps = {
  activeColor: 'green'
  // ...
};

Since you're using Typescript you can create an interface as type-helper and autocompletion.

import React from 'react'

export interface myTypes {
  activeColor: string;
  color: string;
  fontFamily: string;
  fontSize: number;
  fontWeight: string | number;
  height: number;
  icon: React.ReactNode;
  iconOverlay: React.ReactNode;
  marginBottom: number;
  marginLeft: number;
  marginRight: number;
  marginTop: number;
  maxHeight: number;
  minHeight: number;
  onBlur: () => void;
  onChangeText: () => void;
  paddingBottom: number;
  paddingLeft: number;
  paddingRight: number;
  paddingTop: number;
}
import { myTypes } from "my-types-interface";

const dark_theme_properties: myTypes = {
  activeColor: "green",
  ...
};

https://fettblog.eu/typescript-react/prop-types/

import PropTypes, { InferProps } from "prop-types";
import React from 'react';


function Article({
  title,
  price
}: InferProps<typeof Article.propTypes>) {
  return (
    <div className="article">
      <h1>{title}</h1>
      <span>Priced at (incl VAT): {price * 1.2}</span>
    </div>
  );
}

Article.propTypes = {
  title: PropTypes.string.isRequired,
  price: PropTypes.number.isRequired
};

export default Article;
发布评论

评论列表(0)

  1. 暂无评论