benjamn / benjamn/ast-types

Cannot assign a Comment[] to node.comments.

Open
#367 4 comments 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
1.2k
Forks
194
Avg merge
22h 43m
Merged PRs (30d)
10

Description

This code:

function setComment(node: types.Node, comment: types.Comment) {
node.comments = [comment];
}

Results in the following error:
error TS2322: Type 'Comment[]' is not assignable to type 'CommentKind[]'.
Type 'Comment' is not assignable to type 'CommentKind'.
Property 'type' is missing in type 'Comment' but required in type 'CommentLine'.

Adding a "type" field to Comment doesn't help...

error TS2322: Type 'Comment[]' is not assignable to type 'CommentKind[]'.
Type 'Comment' is not assignable to type 'CommentKind'.
Type 'Comment' is not assignable to type 'CommentLine'.
Types of property 'type' are incompatible.
Type 'string' is not assignable to type '"CommentLine"'.

I believe the only resolution is to use CommentKind as the type in the function. Why are there both Command and CommentKind types? Wouldn't it be better if node.comments was of type Comment[]? Also, are the kinds exported at all by ast-types? I could only import them from 'ast-types/gen/kinds' which doesn't feel like an external api.

There are of course similar problems with Statement/StatementKind, Expression/ExpressionKind, etc. Any time code wants to directly mutate a node it cannot work with types of the base class and must always use kinds or leaf classes.

Contributor guide

No contributing guide indexed for this repository

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.