Why are some properties of metadataCache only available in JS and not TS?

While attempting to develop my first plugin, I have run into the issue of finding a cache of all links (full links, including header tags, with info on where in a document they are found, not just resolvedLinks). Given that the API documentation is so sparse, I searched the properties of this.app.metadataCache in the developer console, where I found the getLinks() method. It seems to work to use this in my main.ts file, but this method is missing from the documentation and my IDE indicates that no such method exists.

I found this comment, which seems to indicate that the getLinks() method and many more exist once the .ts is built into .js. Here are my questions:

  1. Is using getLinks() in main.ts going to cause any problems?
  2. Why the discrepancy between what is available in .ts and .js?
  3. Is there a better way to access the complete link information found in getLinks()?


I’d be glad to help you with the issues you’re encountering while developing your first plugin:

Using getLinks() in main.ts:

Potential Issues: While getLinks() might seem to work in your main.ts file initially, it’s not recommended for a few reasons:
Type Safety: TypeScript offers type checking, and getLinks() might not be properly typed, leading to potential errors later.
Maintainability: Using undocumented methods can make your code harder to understand and maintain for yourself and others.
API Changes: If the API changes in the future, using undocumented methods can break your plugin.
Discrepancy Between .ts and .js:

Reason: This is likely due to type definitions. TypeScript relies on separate type definition files (.d.ts) to provide information about HealthCare gov available functions and properties. These definitions might not be complete or might lag behind the actual implementation in JavaScript.
Better Approach:

Check for Official Documentation: Look for official documentation or community resources related to the plugin development framework you’re using. There might be a documented way to access the link information you need.
Investigate Alternative Methods: The framework might offer alternative methods or properties to achieve your goal of getting complete link information. Explore the available options in the documented API.
Consider Feature Requests: If you can’t find a documented way and the undocumented method seems essential, consider making a feature request to the framework developers to expose it officially with proper typing.
Why Some Properties Are Missing in TypeScript:

Incomplete Type Definitions: As mentioned earlier, type definitions for external libraries or frameworks might not always be complete or up-to-date.
Internal Implementation Details: Some properties or methods might be intended for internal use only and not meant for public consumption.
General Recommendations:

Rely on Documented APIs: Whenever possible, stick to documented APIs to ensure type safety, maintainability, and compatibility with future changes.
Explore Alternatives: Look for alternative documented methods or properties that provide the functionality you need.
Consider Feature Requests: If there’s a missing feature that would be valuable for your plugin, consider making a feature request to the framework developers.
Community Resources: Search online forums and communities related to the framework for potential solutions or workarounds.
By following these guidelines, you can develop more robust and maintainable plugins. If you can provide more details about the specific framework you’re using, I might be able to offer more tailored advice.

I hope the information may help you.

1 Like

I appreciate your help with this, especially given my inexperience in TS. I was hoping to develop a plugin that could create a graph view of links between headers (as discussed in this post), and my main roadblock that remains is knowing how to get access to the actual links (e.g., “mydoc#myheader”, not just “mydoc” as seen with resolvedLinks) and their locations in-document to identify which header the link is coming from (also missing from resolvedLinks). So, given that this information can only be found with the getLinks() method in JS, I might be stuck til further development is made.

I think you can just use it. If your IDE complains about this, add a // @ts-ignore above this line

So many plugins use undocumented method, if this plugin is for your own useage, just use it until it’s broken.

Typescript / Your IDE say “no such method exists” just because this method is not “documented”. But it dose exist in obsidian.

1 Like