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

ReadonlyArray-type is not properly recognized #1305

Closed
Etienne-Buschong opened this issue Jul 11, 2022 · 2 comments
Closed

ReadonlyArray-type is not properly recognized #1305

Etienne-Buschong opened this issue Jul 11, 2022 · 2 comments
Labels

Comments

@Etienne-Buschong
Copy link

Etienne-Buschong commented Jul 11, 2022

Describe the bug

Given a node of type ReadonlyArray<...>, the corresponding type is any.

Version: 15.1.0

To Reproduce

import { Project } from "ts-morph";

const project = new Project();
project.createSourceFile('test.ts', 'const x: ReadonlyArray<string> = [];');
const child: Node | undefined = project.getSourceFile('test.ts')?.getFirstChild();

console.log("Is array:", child?.getType().isArray()); // outputs "false"
console.log("Type text:", child?.getType().getText()); // outputs "any"
console.log("Type parameters: ", child?.getType().getTypeArguments()) // outputs "[]"

Expected behavior

The code above outputs false (for isArray()), any (for getText()) and [] (for getTypeArguments()).
The expected behaviour is that:

  • isArray() outputs true. If not, how is an readonlyarray properly identified?
  • getText() outputs ReadonlyArray<string>
  • getTypeArguments() outputs an array of length 1
@dsherret
Copy link
Owner

Part of the reason this is occuring is because calling getFirstChild() on a source file will return its SyntaxList.

See and select Options (in top right corner) > Tree mode > getChildren():

https://ts-ast-viewer.com/#code/MYewdgzgLgBAHgLhgJQKYEMAm4A2BPAQQCcj08AeaIgSzAHMA+GAXhgG0BdAbiA

In this case, you want to get the variable declaration:

const project = new Project();
const sourceFile = project.createSourceFile('test.ts', 'const x: ReadonlyArray<string> = [];');
const varDecl = sourceFile.getVariableDeclarationOrThrow("x");

varDecl.getType().getText(); // "readonly string[]"
varDecl.getType().getTypeArguments().map(t => t.getText()); // [ "string" ]
varDecl.getType().getSymbolOrThrow().getName(); // "ReadonlyArray"

All this said, it does look like .isArray() returns false because the symbol doesn't have the name Array, but it should return it for this as well I think. Overall, it looks like the implementation of that method should be improved to be a bit more robust. I opened #1306 for that.

@Etienne-Buschong
Copy link
Author

Etienne-Buschong commented Jul 12, 2022

Regarding #1306: Would it make sense to add a distinct function isReadonlyArray() on types? Because ReadonlyArray is commonly used in many projects and it may makes sense to have a differentiation for them.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants