-
Notifications
You must be signed in to change notification settings - Fork 2k
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
Upgrade issues with TypeScript enums and nominal typing #3356
Comments
Why not use the const TYPENAME_FIELD: FieldNode = {
- kind: 'Field',
+ kind: Kind.FIELD,
name: {
- kind: 'Name',
+ kind: Kind.NAME,
value: '__typename',
},
};
Exporting |
I’m not sure I want to import a file from graphql-js just to access the enum value. And in the case of
I agree it could be a pain to deal with and write. I still think the nominal typing part about enums is tricky. Any other solution which preserves the structural typing behavior and also allows for dot notation access would be acceptable to me. |
@brainkim Practically speaking is it a blocker for adding v16 support to
It looks like you are using graphql-js/src/language/visitor.ts Lines 185 to 187 in 30b4469
If you have any idea on how to address this happy to implement it in previous versions.
Const enums are ideal for this case however we can't do |
Could we do something like this, I wonder? export const Kind = {
OBJECT: 'ObjectType' as const,
OBJECT_FIELD: 'ObjectField' as const
};
export type Kind = typeof Kind[keyof typeof Kind];
const a: Kind = Kind.OBJECT;
const b: Kind = 'ObjectType';
export interface ASTNode {
kind: typeof Kind.OBJECT
} |
Wow, that is neat! |
@IvanGoncharov It’s not a blocker! Thankfully with TypeScript there are workarounds. Just surfacing the struggle in case others having it. @benjie You can also use the export const Kind = Object.freeze({
OBJECT: 'ObjectType',
OBJECT_FIELD: 'ObjectField',
} as const);
export type Kind = typeof Kind[keyof typeof Kind];
const a: Kind = Kind.OBJECT;
const b: Kind = 'ObjectType';
export interface ASTNode {
kind: typeof Kind.OBJECT
} |
Another detail is that we’re using enum Foo {
a = 1,
b = 2,
}
type FooLegacy = typeof Foo;
const foo: Foo = Foo.a;
// this errors
const foo1: FooLegacy = Foo.a; |
Tracking in newer issue #4253 |
These are two distinct issues, let's leave this open. |
This gets rid of enums for two reasons - TypeScript does not really compile them size efficiently, see [here](https://www.runpkg.com/[email protected]/language/kinds.mjs) - Raw size kinds.js before: 2800bytes - Raw size kinds.js after: 2200 bytes - It's not kind to plains strings as seen in #3356 Resolves #3356 This does not intend to fix the tree-shaking issue as seen in #4253, for that we'd need to fully normalize these exports and sacrifice the DX of the namespaces. I do think that it becomes easier as plain strings will be "easier" compared to before so if you're not doing comparisons you can opt out of the `Kind` namespace. <details> <summary>See new output</summary> ```js /** * The set of allowed kind values for AST nodes. */ exports.Kind = { /** Name */ NAME: 'Name', /** Document */ DOCUMENT: 'Document', OPERATION_DEFINITION: 'OperationDefinition', VARIABLE_DEFINITION: 'VariableDefinition', SELECTION_SET: 'SelectionSet', FIELD: 'Field', ARGUMENT: 'Argument', FRAGMENT_ARGUMENT: 'FragmentArgument', /** Fragments */ FRAGMENT_SPREAD: 'FragmentSpread', INLINE_FRAGMENT: 'InlineFragment', FRAGMENT_DEFINITION: 'FragmentDefinition', /** Values */ VARIABLE: 'Variable', INT: 'IntValue', FLOAT: 'FloatValue', STRING: 'StringValue', BOOLEAN: 'BooleanValue', NULL: 'NullValue', ENUM: 'EnumValue', LIST: 'ListValue', OBJECT: 'ObjectValue', OBJECT_FIELD: 'ObjectField', /** Directives */ DIRECTIVE: 'Directive', /** Types */ NAMED_TYPE: 'NamedType', LIST_TYPE: 'ListType', NON_NULL_TYPE: 'NonNullType', /** Type System Definitions */ SCHEMA_DEFINITION: 'SchemaDefinition', OPERATION_TYPE_DEFINITION: 'OperationTypeDefinition', /** Type Definitions */ SCALAR_TYPE_DEFINITION: 'ScalarTypeDefinition', OBJECT_TYPE_DEFINITION: 'ObjectTypeDefinition', FIELD_DEFINITION: 'FieldDefinition', INPUT_VALUE_DEFINITION: 'InputValueDefinition', INTERFACE_TYPE_DEFINITION: 'InterfaceTypeDefinition', UNION_TYPE_DEFINITION: 'UnionTypeDefinition', ENUM_TYPE_DEFINITION: 'EnumTypeDefinition', ENUM_VALUE_DEFINITION: 'EnumValueDefinition', INPUT_OBJECT_TYPE_DEFINITION: 'InputObjectTypeDefinition', /** Directive Definitions */ DIRECTIVE_DEFINITION: 'DirectiveDefinition', /** Type System Extensions */ SCHEMA_EXTENSION: 'SchemaExtension', /** Type Extensions */ SCALAR_TYPE_EXTENSION: 'ScalarTypeExtension', OBJECT_TYPE_EXTENSION: 'ObjectTypeExtension', INTERFACE_TYPE_EXTENSION: 'InterfaceTypeExtension', UNION_TYPE_EXTENSION: 'UnionTypeExtension', ENUM_TYPE_EXTENSION: 'EnumTypeExtension', INPUT_OBJECT_TYPE_EXTENSION: 'InputObjectTypeExtension', }; ``` </details> Currently seeing whether we should disable the redeclare lint-rule and whether there is a better way to type our `nodeKinds`
This will be solved in v17 after #4254 |
Hullo! I’m upgrading some Apollo projects to GraphQL 16 and running into slight difficulties with enums. The context is that we have utilities which create or modify AST nodes:
The code is semantically sound, but TypeScript requires the assertion because you must reference enums directly wherever they are required (nominal typing).
The disadvantages of this change in GraphQL 16 are:
As a specific example of an enum-based difficulty,
OperationTypeNode
is an enum in GraphQL 16, but it’s not exported as a value in GraphQL 15, so there is no way to reference the enum value across 15 and 16, and you get type errors if you assign strings like"query"
or"mutation"
to the enum.I think the above two concerns are reason enough to switch to using string unions, which are structurally typed, and don’t require direct reference. We can make this a non-breaking change by exporting a namespace instead of an enum, and shadowing the identifier on the type-level with a string union.
Thanks for reading. Let me know your thoughts!
The text was updated successfully, but these errors were encountered: