obsidianmd / obsidian-sample-plugin

2.51k stars 861 forks source link

Error building sample plugin to confirm installation after upgrading to non-deprecated node.js modules #115

Open devinhedge opened 2 weeks ago

devinhedge commented 2 weeks ago

Ran 'git clone https://github.com/obsidianmd/obsidian-sample-plugin.git' Ran 'npm install --save-dev' and received a deprecation error for server packages.

npm warn deprecated inflight@1.0.6: This module is not supported, and leaks memory. Do not use it. Check out lru-cache if you want a good and tested way to coalesce async requests by a key value, which is much more comprehensive and powerful.
npm warn deprecated @humanwhocodes/config-array@0.11.14: Use @eslint/config-array instead
npm warn deprecated rimraf@3.0.2: Rimraf versions prior to v4 are no longer supported
npm warn deprecated glob@7.2.3: Glob versions prior to v9 are no longer supported
npm warn deprecated @humanwhocodes/object-schema@2.0.3: Use @eslint/object-schema instead

I ran 'npm run dev' and every worked fine. If I upgrade the deprecated modules I receive the following:

I'm building another obsidian.md plugin usnig typescript. Attached is my 'main.ts' file. When I type 'npm run build' I receive the following errors:

src/main.ts:14:2 - error TS2564: Property 'settings' has no initializer and is not definitely assigned in the constructor.

14  settings: MyPluginSettings;
    ~~~~~~~~

src/main.ts:43:4 - error TS2322: Type '(editor: Editor, view: MarkdownView) => void' is not assignable to type '(editor: Editor, ctx: MarkdownView | MarkdownFileInfo) => any'.
  Types of parameters 'view' and 'ctx' are incompatible.
    Type 'MarkdownView | MarkdownFileInfo' is not assignable to type 'MarkdownView'.
      Type 'MarkdownFileInfo' is missing the following properties from type 'MarkdownView': previewMode, currentMode, getViewType, getMode, and 39 more.

43    editorCallback: (editor: Editor, view: MarkdownView) => {
      ~~~~~~~~~~~~~~

  node_modules/obsidian/obsidian.d.ts:726:5
    726     editorCallback?: (editor: Editor, ctx: MarkdownView | MarkdownFileInfo) => any;
            ~~~~~~~~~~~~~~
    The expected type comes from property 'editorCallback' which is declared here on type 'Command'

Found 2 errors in the same file, starting at: src/main.ts:14

This fix resolves the issue.

Explanation:

  1. Property 'settings' has no initializer and is not definitely assigned in the constructor:

This error occurs because TypeScript expects the settings property to be initialized in the constructor or declared with a definite assignment assertion. To fix this, you can initialize settings with a default value or mark it with a definite assignment assertion (!).

  1. Type '(editor: Editor, view: MarkdownView) => void' is not assignable to type '(editor: Editor, ctx: MarkdownView | MarkdownFileInfo) => any':

This error occurs because the type signature for editorCallback in your Command expects the second parameter to be either a MarkdownView or MarkdownFileInfo. However, your callback currently only handles MarkdownView. You'll need to update the type signature to accommodate both types or ensure the correct type handling.

devinhedge commented 2 weeks ago

Key Changes:

Settings Initialization:

Editor Callback Type Handling: