繁体   English   中英

Typescript,接口和变量声明:是什么意思?

[英]Typescript, interfaces and variable declaration: what does it means?

这是 Material-UI 接口的一部分:

export interface DialogProps
  extends StandardProps<ModalProps & Partial<TransitionHandlerProps>, DialogClassKey, 'children'> {
  /**
   * The id(s) of the element(s) that describe the dialog.
   */      
  /**
   * If `true`, the dialog stretches to `maxWidth`.
   *
   * Notice that the dialog width grow is limited by the default margin.
   */
  fullWidth?: boolean;
  /**
   * Determine the max-width of the dialog.
   * The dialog width grows with the size of the screen.
   * Set to `false` to disable `maxWidth`.
   */
  maxWidth?: 'xs' | 'sm' | 'md' | 'lg' | 'xl' | false;
  /**
   * Callback fired when the backdrop is clicked.
   */      
  /**
   * Props applied to the [`Transition`](http://reactcommunity.org/react-transition-group/transition#Transition-props) element.
   */
  TransitionProps?: TransitionProps;
}


我尝试了以下分配(它有效:):

const maxWidth: DialogProps['maxWidth'] = 'lg' 

我的问题是:这意味着什么? 我可以认为它声明了一个新变量,它是 DialogProps 的“子类型”吗? 我在哪里可以找到 typescript 中有关此主题的一些文档?

谢谢

当您像这样访问它时,您可以检索属性的类型: DialogProps['maxWidth']

export interface DialogProps {
  /**
   * The id(s) of the element(s) that describe the dialog.
   */      
  /**
   * If `true`, the dialog stretches to `maxWidth`.
   *
   * Notice that the dialog width grow is limited by the default margin.
   */
  fullWidth: boolean;
  /**
   * Determine the max-width of the dialog.
   * The dialog width grows with the size of the screen.
   * Set to `false` to disable `maxWidth`.
   */
  maxWidth: 'xs' | 'sm' | 'md' | 'lg' | 'xl' | false;     
}


const maxWidth: DialogProps.maxWidth = 'lg' 

console.log(maxWidth);

如果您 hover 红色下划线文本,编译器实际上会告诉您您可以使用[propertyName]检索属性的类型。

我自己不知道,文档在这里: https://github.com/Microsoft/TypeScript/blob/1db4f96fd14fc3de5ae3704e925afd6474cfb8f5/doc/spec.md#4.13

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM