React-样式化组件-打字稿-正确的打字

时间:2018-07-01 21:51:33

标签: typescript typescript-typings styled-components

无法以不会破坏我的库的方式键入以下样式化组件时遇到麻烦。|

下面是正确键入此样式即功能组件的第一次尝试,但是我有一个问题,即该解构类型是“ StyledFromReact”还是“ any”,它仍会编译我的组件库键入错误的非相对乱码路径。

要重现这一点,您需要在具有样式的功能组件上运行TSC(带有声明)。我如何正确输出正确的输入?同时仍然是类型安全的?

export type StyledFromReact =
    DOMAttributes<any> &
    {tag: keyof ReactHTML} &
    {children: React.ReactNode[] | React.ReactNode};

export const InkRippleBase =
    styled(({tag = "div", children, ...props}: StyledFromReact) =>
    React.createElement(tag, props, children))`
        overflow: hidden;
        position: relative;
    `;

//编译为......键入错误

export declare const InkRippleBase: import("../../../../../../../../Users/shanon.jackson/Desktop/projects/complibn/node_modules/styled-components").StyledComponentClass<StyledFromReact, import("../../../../../../../../Users/shanon.jackson/Desktop/projects/complibn/src/styled-components/themes/Theme").UbiquityThemes, Pick<StyledFromReact, "children" | "dangerouslySetInnerHTML" | "onCopy" | "onCopyCapture" | "onCut" | "onCutCapture" | "onPaste" | "onPasteCapture" | "onCompositionEnd" | "onCompositionEndCapture" | "onCompositionStart" | "onCompositionStartCapture" | "onCompositionUpdate" | "onCompositionUpdateCapture" | "onFocus" | "onFocusCapture" | "onBlur" | "onBlurCapture" | "onChange" | "onChangeCapture" | "onInput" | "onInputCapture" | "onReset" | "onResetCapture" | "onSubmit" | "onSubmitCapture" | "onInvalid" | "onInvalidCapture" | "onLoad" | "onLoadCapture" | "onError" | "onErrorCapture" | "onKeyDown" | "onKeyDownCapture" | "onKeyPress" | "onKeyPressCapture" | "onKeyUp" | "onKeyUpCapture" | "onAbort" | "onAbortCapture" | "onCanPlay" | "onCanPlayCapture" | "onCanPlayThrough" | "onCanPlayThroughCapture" | "onDurationChange" | "onDurationChangeCapture" | "onEmptied" | "onEmptiedCapture" | "onEncrypted" | "onEncryptedCapture" | "onEnded" | "onEndedCapture" | "onLoadedData" | "onLoadedDataCapture" | "onLoadedMetadata" | "onLoadedMetadataCapture" | "onLoadStart" | "onLoadStartCapture" | "onPause" | "onPauseCapture" | "onPlay" | "onPlayCapture" | "onPlaying" | "onPlayingCapture" | "onProgress" | "onProgressCapture" | "onRateChange" | "onRateChangeCapture" | "onSeeked" | "onSeekedCapture" | "onSeeking" | "onSeekingCapture" | "onStalled" | "onStalledCapture" | "onSuspend" | "onSuspendCapture" | "onTimeUpdate" | "onTimeUpdateCapture" | "onVolumeChange" | "onVolumeChangeCapture" | "onWaiting" | "onWaitingCapture" | "onClick" | "onClickCapture" | "onContextMenu" | "onContextMenuCapture" | "onDoubleClick" | "onDoubleClickCapture" | "onDrag" | "onDragCapture" | "onDragEnd" | "onDragEndCapture" | "onDragEnter" | "onDragEnterCapture" | "onDragExit" | "onDragExitCapture" | "onDragLeave" | "onDragLeaveCapture" | "onDragOver" | "onDragOverCapture" | "onDragStart" | "onDragStartCapture" | "onDrop" | "onDropCapture" | "onMouseDown" | "onMouseDownCapture" | "onMouseEnter" | "onMouseLeave" | "onMouseMove" | "onMouseMoveCapture" | "onMouseOut" | "onMouseOutCapture" | "onMouseOver" | "onMouseOverCapture" | "onMouseUp" | "onMouseUpCapture" | "onSelect" | "onSelectCapture" | "onTouchCancel" | "onTouchCancelCapture" | "onTouchEnd" | "onTouchEndCapture" | "onTouchMove" | "onTouchMoveCapture" | "onTouchStart" | "onTouchStartCapture" | "onScroll" | "onScrollCapture" | "onWheel" | "onWheelCapture" | "onAnimationStart" | "onAnimationStartCapture" | "onAnimationEnd" | "onAnimationEndCapture" | "onAnimationIteration" | "onAnimationIterationCapture" | "onTransitionEnd" | "onTransitionEndCapture" | "tag"> & {
    theme?: import("../../../../../../../../Users/shanon.jackson/Desktop/projects/complibn/src/styled-components/themes/Theme").UbiquityThemes;
}>;

这也与我在外部库中使用样式组件时遇到的另一个问题有关,如果我有一个带有隐式返回类型的函数,该函数返回样式组件的css,我在输出类型中也会得到乱码的非相对路径名。

IE

const test = () => css``;

当TSC用声明进行编译时,产生的结果与上述类似,它使用了一种变通方法iv'e,基本上只是显式地添加了正确的类型注释,而函数式样式并没有我似乎找到了一种解决方法。

0 个答案:

没有答案
相关问题