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

Type CommentJSONValue object properties #42

Open
felixhaeberle opened this issue Sep 18, 2023 · 2 comments
Open

Type CommentJSONValue object properties #42

felixhaeberle opened this issue Sep 18, 2023 · 2 comments
Labels

Comments

@felixhaeberle
Copy link

Tell us about your environment

I'm running this inside a vscode extension to parse the extensions.json

  • Node Version: 18.16.1
  • comment-json Version 4.2.3

Please show your use case / code slices / code link that could reproduce the issue

I want to have the returned value of parse correctly typed. It should return an json with recommendations as string[], but I can't get it to work.

let extensions: CommentJSONValue<{ recommendations: string[] }> |  undefined // doesn't work
extensions = parse(fs.readFileSync(extensionsJsonPath, "utf8"))
extensions?.recommendations?.includes("example.extension") || false // doesn't work

What did you expect to happen?

I want to have the recommendations typed correctly.
Thank you for your help!!

@felixhaeberle felixhaeberle changed the title Type ´CommentJSONValue` object properties Type CommentJSONValue object properties Sep 18, 2023
@stevebeauge
Copy link

In a project I'm working on, I had to parse tsconfig.json file, which can contains comments. The purpose of my project was to sync ts references and paths in a monorepo.

This library lacks strong typing support (or at least I didn't understood how to). What I did:

import { assign, CommentArray, CommentObject, parse as jsonCParse, stringify } from "comment-json";

type Reference = Partial<CommentObject> & {
    path: string;
};

type CompilerOptions = Partial<CommentObject> & {
    paths?: Record<string, CommentArray<string>>;
};

// Only the actual nodes I need
type TSConfigWithComments = Partial<CommentObject> & {
    references?: Reference[];
    compilerOptions?: CompilerOptions;
};

const tsConfigFile = await readFileAsync(projectData.tsConfigFile);

const tsConfig = jsonCParse(tsConfigFile) as TSConfigWithComments; // The only required cast here
if (tsConfig === undefined) {
    throw new Error(`Error parsing ${projectData.tsConfigFile}`);
}

if (!tsConfig.references) { // create property if not present
    assign(tsConfig, { references: [] });
}
if (!tsConfig.references) { // Actually useless, but makes TS know the references is not undefined (strictNullChecks enabled)
    throw new Error("references should be defined");
}
tsConfig.references.push({ path: someppath; }) // Add reference node

// Ensure compilerOptions.path is present
if (!tsConfig.compilerOptions) {
    assign(tsConfig, { compilerOptions: { paths: {} } });
}
if (!tsConfig.compilerOptions?.paths) {
    assign(tsConfig.compilerOptions, { paths: {} });
}
// Makes TS know it's not undefined
if (!tsConfig.compilerOptions?.paths) {
    throw new Error("compilerOptions.path should be defined");
}
tsConfig.compilerOptions.paths[`${someName}/*`] = new CommentArray(`${somePath}/src/*`);

const newJson = stringify(tsConfig, undefined, 2);

Not sure if it's the proper way to handle it, but maybe it can helps?

@felixhaeberle
Copy link
Author

@stevebeauge Thanks! Will look into it.

jace-roell added a commit to zowe/zowe-cli that referenced this issue Jun 4, 2024
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