Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

HOC typing example seems to be no longer valid as of TS@3.2+ #72

Closed
infctr opened this issue Jan 17, 2019 · 7 comments
Closed

HOC typing example seems to be no longer valid as of TS@3.2+ #72

infctr opened this issue Jan 17, 2019 · 7 comments

Comments

@infctr
Copy link

infctr commented Jan 17, 2019

image

typescript@3.2.2
@types/react@16.7.20

The code from the Advanced section

Type '{ prop: string; } & Readonly<{ children?: ReactNode; }> & Readonly<Pick<T, Exclude<keyof T, "prop">>>' is not assignable to type 'IntrinsicAttributes & T & { children?: ReactNode; }'.
  Type '{ prop: string; } & Readonly<{ children?: ReactNode; }> & Readonly<Pick<T, Exclude<keyof T, "prop">>>' is not assignable to type 'T'.ts(2322)
import React from 'react'

type WithThemeProps = {
  prop: string;
}

type Omit<T, K extends keyof T> = Pick<T, Exclude<keyof T, K>>;
type Optionalize<T extends K, K> = Omit<T, keyof K>;

export function withTheme<T extends WithThemeProps = WithThemeProps>(WrappedComponent: React.ComponentType<T>) {
  // Try to create a nice displayName for React Dev Tools.
  const displayName = WrappedComponent.displayName || WrappedComponent.name || "Component";

  // Creating the inner component. The calculated Props type here is the where the magic happens.
  return class ComponentWithTheme extends React.Component<Optionalize<T, WithThemeProps>> {
    public static displayName = `withPages(${displayName})`;

    public render() {
      // this.props comes afterwards so the can override the default ones.
      return <WrappedComponent prop="prop" {...this.props} />;
    }
  }
}
@ferdaber
Copy link
Collaborator

Yeah this is a bug in the compiler after some changes in 3.2. I'll see if I can find the issue in the TS repo.

@ferdaber
Copy link
Collaborator

Workaround is to do {...this.props as T}

@infctr
Copy link
Author

infctr commented Jan 17, 2019

@ferdaber thnx, I got really puzzled after ts update

@eps1lon
Copy link
Member

eps1lon commented Jan 17, 2019

Looks like microsoft/TypeScript#28938 with workaround described in microsoft/TypeScript#28938 (comment)

@ferdaber
Copy link
Collaborator

That's the one!

@swyxio
Copy link
Collaborator

swyxio commented Jan 17, 2019

anything we can do to fix in the meantime? maybe add a link?

@infctr infctr closed this as completed Jan 17, 2019
@adamborowski
Copy link

Workaround is to do {...this.props as T}

@ferdaber do you have any docs explaining why?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants