如何将流转换为打字稿

时间:2018-12-23 21:34:13

标签: typescript flowtype

嗨,我有这段代码,我想从流转换为打字稿。到目前为止,我的进展是将类型转换为接口,但是我被困在这里

// @flow
export type AlertType = 'success'

export type AlertProps = {
  type: AlertType,
  text: string,
  testId: string,
}

type AlertTypeIconMap = {
  +[AlertType]: IconType,
}

const Alert = ({ type, text, testId }: AlertProps) => {
  const alertTypeIconMap: AlertTypeIconMap = {
    success: 'tick',
  }
  const styles = getStyles({ type })

  return (
      <View style={styles.iconContainer}>
        <Icon type={alertTypeIconMap[type]} />
      </View>
  )
}

export default Alert

到目前为止,我的进度是

export type AlertType = 'success'

export interface AlertProps {
  type: AlertType
  text: string
  testId: string
}

interface AlertTypeIconMap {
  +[AlertType]: IconType
}

我在这行出现错误

interface AlertTypeIconMap {
  +[AlertType]: IconType
}

说“ AlertType”仅指一种类型,但在此处被用作值

2 个答案:

答案 0 :(得分:0)

TypeScript语法类似于[k in AlertType]

// assume IconType
type IconType = '';

export type AlertType = 'success';

export type AlertTypeIconMap = { [k in AlertType]: IconType };

答案 1 :(得分:0)

我使用了flow-to-ts

yarn global add @khanacademy/flow-to-ts
flow-to-ts --write --delete-source ${myProjectPath}/src/**/**.js

还有其他选择:

Usage: flow-to-ts [options]

Options:
  -V, --version                    output the version number
  --inline-utility-types           inline utility types when possible, defaults to 'false'
  --prettier                       use prettier for formatting
  --semi                           add semi-colons, defaults to 'false' (depends on --prettier)
  --single-quote                   use single quotes instead of double quotes, defaults to 'false' (depends on --prettier)
  --tab-width [width]              size of tabs (depends on --prettier) (default: 4)
  --trailing-comma [all|es5|none]  where to put trailing commas (depends on --prettier) (default: "all")
  --bracket-spacing                put spaces between braces and contents, defaults to 'false' (depends on --prettier)
  --arrow-parens [avoid|always]    arrow function param list parens (depends on --prettier) (default: "avoid")
  --print-width [width]            line width (depends on --prettier) (default: 80)
  --write                          write output to disk instead of STDOUT
  --delete-source                  delete the source file
  -h, --help                       output usage information