博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
TypeScript基础入门之JSX(二)
阅读量:7082 次
发布时间:2019-06-28

本文共 3676 字,大约阅读时间需要 12 分钟。

hot3.png

转发 

属性类型检查

键入检查属性的第一步是确定元素属性类型。

内在元素和基于价值的元素之间略有不同。

对于内部元素,它是JSX.IntrinsicElements上的属性类型

declare namespace JSX {  interface IntrinsicElements {    foo: { bar?: boolean }  }}// element attributes type for 'foo' is '{bar?: boolean}'
;

对于基于价值的元素,它有点复杂。

它由先前确定的元素实例类型上的属性类型确定。
使用哪个属性由JSX.ElementAttributesProperty确定。
它应该用单个属性声明。
然后使用该属性的名称。
从TypeScript 2.8开始,如果未提供JSX.ElementAttributesProperty,则将使用类元素的构造函数或SFC调用的第一个参数的类型。

declare namespace JSX {  interface ElementAttributesProperty {    props; // specify the property name to use  }}class MyComponent {  // specify the property on the element instance type  props: {    foo?: string;  }}// element attributes type for 'MyComponent' is '{foo?: string}'

元素属性类型用于键入检查JSX中的属性。

支持可选和必需的属性。

declare namespace JSX {  interface IntrinsicElements {    foo: { requiredProp: string; optionalProp?: number }  }}
; // ok
; // ok
; // error, requiredProp is missing
; // error, requiredProp should be a string
; // error, unknownProp does not exist
; // ok, because 'some-unknown-prop' is not a valid identifier

注意:如果属性名称不是有效的JS标识符(如data-*属性),则如果在元素属性类型中找不到它,则不会将其视为错误。

此外,JSX.IntrinsicAttributes接口可用于指定JSX框架使用的额外属性,这些属性通常不被组件的props或参数使用 - 例如React中的键。

进一步说,通用JSX.IntrinsicClassAttributes <T>类型也可用于为类组件(而不是SFC)指定相同类型的额外属性。
在此类型中,泛型参数对应于类实例类型。
在React中,这用于允许类型为Ref <T>的ref属性。
一般来说,这些接口上的所有属性都应该是可选的,除非您打算让JSX框架的用户需要在每个标记上提供一些属性。
扩展操作符也是有效的:

var props = { requiredProp: "bar" };
; // okvar badProps = {};
; // error

子类型检查

在TypeScript 2.3中,TS引入了子类型检查。

children是元素属性类型中的特殊属性,其中子JSXExpressions被插入到属性中。
类似于TS使用JSX.ElementAttributesProperty来确定props的名称,TS使用JSX.ElementChildrenAttribute来确定这些props中的子项名称。
应使用单个属性声明JSX.ElementChildrenAttribute。

declare namespace JSX {  interface ElementChildrenAttribute {    children: {};  // specify children name to use  }}
 

Hello

;
 

Hello

  World
;const CustomComp = (props) =>
props.children
 
Hello World
  {"This is just a JS expression..." + 1000}

您可以像任何其他属性一样指定子类型。
这将覆盖默认类型,例如React typings(如果您使用它们)。

interface PropsType {  children: JSX.Element  name: string}class Component extends React.Component
{  render() {    return (     

        {this.props.children}     

    )  }}// OK
 

Hello World

// Error: children is of type JSX.Element not array of JSX.Element
 

Hello World

 

Hello World

// Error: children is of type JSX.Element not array of JSX.Element or string.
 

Hello

  World

JSX结果类型

默认情况下,JSX表达式的结果键入为any。您可以通过指定JSX.Element接口来自定义类型。但是,无法从此接口检索有关JSX的元素,属性或子级的类型信息。这是一个黑盒子。

嵌入表达式

JSX允许您通过用大括号({})包围表达式来在标记之间嵌入表达式。

var a = 
  {["foo", "bar"].map(i =>
{i / 2})}

上面的代码将导致错误,因为您不能将字符串除以数字。

使用preserve选项时,输出如下所示:

var a = 
  {["foo", "bar"].map(function (i) { return
{i / 2}; })}

React整合

要将JSX与React一起使用,您应该使用React类型。

这些类型适当地定义了JSX名称空间以与React一起使用。

/// 
interface Props {  foo: string;}class MyComponent extends React.Component
{  render() {    return
{this.props.foo}  }}
; // ok
; // error

工厂函数

jsx:react编译器选项使用的确切工厂函数是可配置的。

可以使用jsxFactory命令行选项或内联@jsx注释编译指示来设置它以基于每个文件进行设置。
例如,如果将jsxFactory设置为createElement,则<div />将作为createElement("div")而不是React.createElement("div")来编译。

注释pragma版本可以像这样使用(在TypeScript 2.8中):

import preact = require("preact");/* @jsx preact.h */const x = 
;

编译为

const preact = require("preact");const x = preact.h("div", null);

选择的工厂也将影响JSX命名空间的查找位置(用于类型检查信息),然后再回退到全局命名空间。

如果工厂定义为React.createElement(默认值),编译器将在检查全局JSX之前检查React.JSX。
如果工厂定义为h,它将在全局JSX之前检查h.JSX。

转载于:https://my.oschina.net/zhangdapeng89/blog/2250044

你可能感兴趣的文章
centos7环境安装rabbitMQ
查看>>
时间换算方法
查看>>
python之参数解析模块argparse
查看>>
touch事件的分发和消费机制
查看>>
Codeforces Round #396 (Div. 2) C. Mahmoud and a Message dp
查看>>
activity 与 service 之间的通信
查看>>
annovar积累
查看>>
JAVA入门[8]-测试mybatis
查看>>
C语言 · 大数乘法
查看>>
lower_bound与upper_bound
查看>>
HBase in Action前三章笔记
查看>>
iOS开发自己定义键盘回车键Return Key
查看>>
大型分布式站点的技术需求
查看>>
vue2
查看>>
SpringMVC系列(四)使用 POJO 对象绑定请求参数值
查看>>
Nginx的https配置记录以及http强制跳转到https的方法梳理
查看>>
Roslyn还出现这么低级的错误,不应该呀!
查看>>
plsql 通过修改配置文件的方式实现数据库的连接
查看>>
VC++调用MSFlexGrid的SetRow方法,出现异常“Invalid Row Value”
查看>>
MD5 Hashing in Java
查看>>