question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

JSX: a way to make all the properties of value-based elements optional

See original GitHub issue

It’s not documented, but currently, if the JSX.ElementAttributesProperty interface is declared empty,

declare namespace JSX { interface ElementAttributesProperty {} }

then the element attributes type becomes same as the element instance type. However, if the element instance type is a class, this leads to a situation where the compiler requires that all the members of the class be represented as attributes since they’re not optional (and even cannot be marked as such as the class syntax doesn’t allow optional members).

class MyComponent {
  myProp: string;
  private myMethod(): void;
}
var a = <MyComponent myProp='value'/>;
// error: Property 'myMethod' is missing in type MyComponent

This prevents me from using JSX for type-checking Angular 1 templates (you can find my experiments in this area here).

What if we introduce some way to tell the compiler that not all the properties are required, but only some of them? E.g. those having a certain decorator?

A related issue: #5151

Issue Analytics

  • State:open
  • Created 8 years ago
  • Reactions:1
  • Comments:23 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
thorn0commented, Nov 27, 2016

Hm…

abstract class Component {
  /** A bogus type-system-only property. */
  private __bogusProps: Partial<this>;
}

declare namespace JSX {
  type Element = string;
  interface ElementAttributesProperty {
    __bogusProps: Partial<this>;
  }
}

class FooComponent extends Component {
  foo: number;
  method() { }
}

// ta-da !
let z = <FooComponent foo={2}/>;
1reaction
vvakamecommented, Nov 27, 2016

Now I am understood what @RyanCavanaugh and @aluanhaddad are said. We can use Partial types (a.k.a Mapped types) now.

We need good proposal.

IMHO, If JSX.ElementAttributesProperty has callable property, Use the return type instead of the type passed as an argument for checking. like this.

interface ElementAttributesProperty {
    ''<T>(v: T): Partial<T>;
}

This issue is last 2 mile of SkateJS+TypeScript. (other 1 is #12488 ) If you have no seen SkateJS yet, please try it once. it’s fun! http://skate.js.org/ https://github.com/vvakame/skatejs-todo/

Read more comments on GitHub >

github_iconTop Results From Across the Web

4 Methods to Add Conditional Attributes to React Components
This method is one of my favorites when it comes to conditional props. The Spread Operator is used when passing all the properties...
Read more >
Optional JSX Props In a TSX/JSX Project - Stack Overflow
An issue I'm running into is that TSX React is assuming that all properties defined in a functional component are required props. //...
Read more >
Documentation - JSX - TypeScript
For value-based elements, it is a bit more complex. It is determined by the type of a property on the element instance type...
Read more >
API - React Select
These base props are those available to be passed to all select variants. ... Formats option labels in the menu and control as...
Read more >
React Props Cheatsheet: 10 Patterns You Should Know
What if we have an object whose properties we want to pass down as individual prop values? This object has a lot of...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found