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

Describe incorrectly handles single literal types. #1240

Open
yeoffrey opened this issue May 31, 2024 · 1 comment
Open

Describe incorrectly handles single literal types. #1240

yeoffrey opened this issue May 31, 2024 · 1 comment

Comments

@yeoffrey
Copy link
Contributor

Description

It seems that using a single literal in an enums struct will not be interpreted by Describe properly when working with TS types that are literals. Is this the wrong usage of enums or describe? If not, I'm willing to submit a PR for this.

Reproduce

Here is a type which is a union of string literals (working).

import {Describe, enums} from 'superstruct';

type MyType = 'foo' | 'bar';

const MyTypeStruct: Describe<MyType> = enums(['foo', 'bar']);

However, when using Describe with a type which is a single string literal, there is an error.

import {Describe, enums} from 'superstruct';

type MyType = 'foo';

const MyTypeStruct: Describe<MyType> = enums(['foo']);

Error:

Type 'Struct<"foo", { foo: "foo"; }>' is not assignable to type 'Describe<"foo">'.
     Type '{ foo: "foo"; }' is not assignable to type '"foo"'. [2322]

Using Infer

This seems to work the other way when using Infer from a struct to generate a type. For example:

import {Infer, enums} from 'superstruct';

const literalEnum = enums(['foo']);

type MyType = Infer<typeof literalEnum>;

const MyLiteral: MyType = 'foo';

Extra info:

Node: v20.14.0
SuperStruct: v1.0.4
I have strict enabled in my TypeScript configuration for these examples.
I'm willing to submit a PR for this.

@yeoffrey
Copy link
Contributor Author

yeoffrey commented Jun 3, 2024

For now, just use literal(). Maybe there should be a suggestion if you create an enums with only one value. Intuitively this to me feels like something that could be streamlined a bit. Is enums just an array of literals?

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

1 participant