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

Type check JSX defaultProps as Partial<Props> #33951

Closed
5 tasks done
iansan5653 opened this issue Oct 11, 2019 · 2 comments
Closed
5 tasks done

Type check JSX defaultProps as Partial<Props> #33951

iansan5653 opened this issue Oct 11, 2019 · 2 comments
Labels
Out of Scope This idea sits outside of the TypeScript language design constraints Suggestion An idea for TypeScript

Comments

@iansan5653
Copy link

iansan5653 commented Oct 11, 2019

Suggestion

Currently, JSX defaultProps are not type-checked; any value can be passed to the properties of this object. One would think that they could fix it by defining the type of defaultProps as Partial<Props> but of course this breaks the default handling of defaultProps where it marks all the props with keys in defaultProps as optional.

I suggest that instead, the compiler should check the value of defaultProps by treating it as Partial<Props> when checking it, but still use the actual value of it when handling it.

Use Cases

This is useful when writing any class-based react components. Currently I could set the default value of any prop to anything and no error will be shown, which is definitely a potential bug.

Examples

interface Props {
  example: number
}

export default class Button extends React.Component<Props> {
  static defaultProps = {
    example: "forty" // This should be an error, but it's not
  };

  render(): React.ReactNode {
    // render
  }
}

Checklist

My suggestion meets these guidelines:

  • This wouldn't be a breaking change in existing TypeScript/JavaScript code (it would raise errors for some code that already has errors)
  • This wouldn't change the runtime behavior of existing JavaScript code
  • This could be implemented without emitting different JS based on the types of the expressions
  • This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • This feature would agree with the rest of TypeScript's Design Goals.
@RyanCavanaugh RyanCavanaugh added Out of Scope This idea sits outside of the TypeScript language design constraints Suggestion An idea for TypeScript labels Oct 30, 2019
@RyanCavanaugh
Copy link
Member

We don't hardcode React logic in the type system.

See #33892 for a proposal that would allow for this.

@iansan5653
Copy link
Author

How is the defaultProps situation handled currently? Is this something built into react itself?

For example:

interface Props {
  requiredProp: string;
}

const Element extends Component<Props> {
  defaultProps = {
    requiredProp: "example"
  }

  render() {
    return <span>{this.props.requiredProp}</span>
    // Here requiredProp has type `string`, not `string | undefined`
  }
}

<Element /> // This is ok because here requiredProp has type `string | undefined`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Out of Scope This idea sits outside of the TypeScript language design constraints Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

2 participants