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

No excess property checks for nullary sums #3

Closed
Magellol opened this issue May 29, 2023 · 1 comment · Fixed by #4
Closed

No excess property checks for nullary sums #3

Magellol opened this issue May 29, 2023 · 1 comment · Fixed by #4
Labels
bug Something isn't working

Comments

@Magellol
Copy link
Member

The following produces a runtime exception with no type errors.

import { getShow } from '@unsplash/sum-types-fp-ts';
import * as Sum from '@unsplash/sum-types';

type S = Sum.Member<'A'> | Sum.Member<'B'>;
const S = Sum.create<S>();

const show = getShow<S>({
  A: 'Custom',
});

console.log(show.show(S.mk.A));

https://stackblitz.com/edit/typescript-5afkt4?file=index.ts

I believe if supplied, A must be an instance of Show but TS doesn't check for it.

Side note, I find it a little confusing prepending the sum member key in the show output here https://github.com/unsplash/sum-types-fp-ts/blob/master/src/index.ts#L153 . In the following code I'm expecting the show output to be CustomA but instead I get A CustomerA

const show = getShow<S>({
  A: {
    show: () => "CustomA"
  }  
});
@Magellol Magellol added the bug Something isn't working label May 29, 2023
@samhh
Copy link
Member

samhh commented May 30, 2023

This appears to only be an issue with nullary sums. Add any non-nullary member and A will be flagged as an unknown property.

Looks like an issue with {} as well:

// no error
const x: {} = {
  B: 123,
}

// unknown property error on `B`
const y: { A: string } = {
  B: 123,
}

That looks like expected (questionable) TS behaviour: microsoft/TypeScript#47486 (comment)

This bug very likely applies to the other exports as well, both here and in other libraries.

@samhh samhh changed the title Runtime exception using getShow and providing wrong type arguments. No excess property checks for nullary sums Aug 11, 2023
@samhh samhh closed this as completed in #4 Jan 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants