UNPKG

dora-ui

Version:

A React.js Mobile UI Library

40 lines (33 loc) 1.37 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createPropsGetter = exports.withDefaultProps = void 0; /* eslint-disable @typescript-eslint/no-unused-vars,no-param-reassign */ // https://medium.com/@martin_hotell/react-typescript-and-defaultprops-dilemma-ca7f81c661c7 /** * 目的:使得默认属性在类型定义中是可选的 * 但我们在组件实现中是必选的 */ var withDefaultProps = function withDefaultProps(defaultProps, Cmp) { // 抽取出必需的props(不含defaultProps的部分) // 重新创建props接口定义,还原原本的接口定义 // 注入defaultProps Cmp.defaultProps = defaultProps; return Cmp; }; /** * 默认属性不报错的另外一种实现方式 * 把可选的defaultProps的类型剔除后,加入必选的defaultProps的类型,从而形成一个新的Props类型, * 这个Props类型中的defaultProps相关属性就变成了必选的 * @param defaultProps 默认属性对象 */ exports.withDefaultProps = withDefaultProps; var createPropsGetter = function createPropsGetter(defaultProps) { return function (props) { // 剔除Props类型中关于defaultProps的部分 // 将默认属性的类型DP与剔除了默认属性的Props类型结合在一起 均为必选 return props; }; }; exports.createPropsGetter = createPropsGetter;