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

@fluentui/react-button: Address polymorphic typings #14414

Closed
dzearing opened this issue Aug 7, 2020 · 4 comments
Closed

@fluentui/react-button: Address polymorphic typings #14414

dzearing opened this issue Aug 7, 2020 · 4 comments

Comments

@dzearing
Copy link
Member

dzearing commented Aug 7, 2020

We need to straighten out typings of Button and other components which use as. There is a way!

Expected:

const props: ButtonProps = {
  as: 'a',
  href: 'url', // Good to go!
};

const props2: ButtonProps = {
  as: 'button',
  href: 'url', // Build error
};

Resulted:

Typings are loose right now, extending React.AllHTMLAttributes.

@dzearing
Copy link
Member Author

I have this working, but if as is not specified, i haven't found a way to default to a particular assumption.

@JustSlone JustSlone added this to the v8: Pre-snap milestone Aug 21, 2020
@JustSlone JustSlone assigned behowell and joschect and unassigned khmakoto Sep 8, 2020
@dzearing dzearing removed their assignment Sep 8, 2020
@joschect
Copy link
Contributor

joschect commented Sep 20, 2020

It is currently impossible to do this if as can be undefined. If as is non-optional then this will mostly work as intended. Here's a typescript playground that demonstrates how this works if as can be undefined in one of the interfaces.

Additionally, here is an issue in the typescript repo that indicates that this is likely a limitation of the language rather than something that can be fixed.

Recommendation, it is possible to make a MutuallyExclusive type that will ensure that a discriminated union is correct. There will need to be some work done to ensure that this works smoothly.

Here is a prototype that works, although it it is inelegant.

type MakeExclusive<T, K extends string> = {
  [P in Exclude<keyof T, K>]?: undefined;
};

@ecraig12345
Copy link
Member

Do we still need this?

@ecraig12345 ecraig12345 removed this from the v8: Pre-release milestone Dec 10, 2020
@khmakoto
Copy link
Member

Folding into #16746.

@microsoft microsoft locked as resolved and limited conversation to collaborators Mar 27, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

8 participants