From b6ffefefca8d17d4d4e87dd238f430713516d43a Mon Sep 17 00:00:00 2001 From: Sun Knudsen Date: Fri, 28 Feb 2020 13:50:06 -0500 Subject: [PATCH] Made ComponentOverride generic (#42603) Fixes type issues such as https://stackoverflow.com/questions/60382399/why-is-type-sfcanchorprops-not-assignable-to-type-sfc. --- types/markdown-to-jsx/index.d.ts | 22 ++-- .../markdown-to-jsx/markdown-to-jsx-tests.tsx | 110 +++++++++++++++--- 2 files changed, 107 insertions(+), 25 deletions(-) diff --git a/types/markdown-to-jsx/index.d.ts b/types/markdown-to-jsx/index.d.ts index 0cb0a370a4..0a704d3e00 100644 --- a/types/markdown-to-jsx/index.d.ts +++ b/types/markdown-to-jsx/index.d.ts @@ -1,12 +1,13 @@ -// Type definitions for markdown-to-jsx 6.9 +// Type definitions for markdown-to-jsx 6.11 // Project: https://probablyup.github.io/markdown-to-jsx // Definitions by: Elizabeth Craig +// Sun Knudsen // Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped // TypeScript Version: 2.8 import * as React from 'react'; -export default class Markdown extends React.Component { } +export default class Markdown extends React.Component {} export interface MarkdownProps extends React.HTMLAttributes { options?: MarkdownOptions; @@ -15,10 +16,14 @@ export interface MarkdownProps extends React.HTMLAttributes { children?: React.ReactNode; } -export type ComponentOverride = string | React.ComponentClass | React.SFC | { - component: string | React.ComponentClass | React.SFC; - props?: any; -}; +export type ComponentOverride

= + | string + | React.ComponentClass + | React.SFC

+ | { + component: string | React.ComponentClass | React.SFC

; + props?: any; + }; export interface MarkdownOptions { /** Force all input strings to use block layout. */ @@ -70,9 +75,10 @@ export interface MarkdownOptions { type: React.SFC

| React.ComponentClass

| string, // This typing is copied from React // tslint:disable-next-line:no-null-undefined-union - props?: React.Attributes & P | null, + props?: (React.Attributes & P) | null, // tslint:disable-next-line:no-null-undefined-union - ...children: React.ReactNode[]) => React.ReactElement

; + ...children: React.ReactNode[] + ) => React.ReactElement

; /** Custom function to generate an HTML id from headings. */ slugify?: (text: string) => string; diff --git a/types/markdown-to-jsx/markdown-to-jsx-tests.tsx b/types/markdown-to-jsx/markdown-to-jsx-tests.tsx index 835f81b31c..76c1803029 100644 --- a/types/markdown-to-jsx/markdown-to-jsx-tests.tsx +++ b/types/markdown-to-jsx/markdown-to-jsx-tests.tsx @@ -9,9 +9,70 @@ compiler('Hello there old chap!', { forceBlock: true }); # You got it babe!; -const MyParagraph: React.FunctionComponent = ({ children, ...props }) => ( -

{children}
-); +const MyParagraph: React.SFC = ({ children, ...props }) =>
{children}
; + +interface MySquareImageProps { + src: string; + alt?: string; + size?: number; +} + +const MySquareImage = (props: MySquareImageProps) => { + let width: string | undefined; + let height: string | undefined; + if (props.size) { + width = `${props.size}px`; + height = `${props.size}px`; + } + return {props.alt}; +}; + +interface MyStatelessRoundImageProps { + src: string; + alt?: string; + size?: number; +} + +class MyStatelessRoundImage extends React.Component { + render() { + let width: string | undefined; + let height: string | undefined; + if (this.props.size) { + width = `${this.props.size}px`; + height = `${this.props.size}px`; + } + return {this.props.alt}; + } +} + +interface MyRoundImageProps { + src: string; + alt?: string; + size?: number; +} + +interface MyRoundImageState { + loading: boolean; +} + +class MyRoundImage extends React.Component { + constructor(props: MyRoundImageProps) { + super(props); + this.state = { + loading: true, + }; + } + render() { + let width: string | undefined; + let height: string | undefined; + if (this.props.size) { + width = `${this.props.size}px`; + height = `${this.props.size}px`; + } + return {this.props.alt}; + } +} + render( # Hello world! - , - document.body + , + document.body, ); render( @@ -47,19 +126,16 @@ render( children="# Hello world" options={{ createElement:

( - type: React.FunctionComponent

| React.ComponentClass

| string, + type: React.SFC

| React.ComponentClass

| string, // This typing is copied from React // tslint:disable-next-line:no-null-undefined-union - props?: React.Attributes & P | null, + props?: (React.Attributes & P) | null, // tslint:disable-next-line:no-null-undefined-union - ...children: React.ReactNode[]) => ( -

- {React.createElement(type, props, children)} -
- ) + ...children: React.ReactNode[] + ) =>
{React.createElement(type, props, children)}
, }} />, - document.body + document.body, ); str }}># 中文;