The TypeScript error message is indicating that the variable is being used as a value, even though it is only defined as a type

I'm encountering a persistent TypeScript error that I can't seem to suppress, not even with @ts-ignore. Oddly enough, the code functions perfectly at runtime when TypeScript is disabled temporarily for testing. Is TypeScript misbehaving in this scenario, and is there a workaround available?

TS: 'HeTreeComponent' only refers to a type, but is being used as a value here.ts(2693)
TS: 'Fold' only refers to a type, but is being used as a value here.ts(2693)
TS: 'Draggable' only refers to a type, but is being used as a value here.ts(2693)
TS: 'foldAll' only refers to a type, but is being used as a value here.ts(2693)
TS: 'Check' only refers to a type, but is being used as a value here.ts(2693)

Below is the code snippet (from a Vue 3 application):

<script setup lang="ts">
    import { Tree as HeTreeComponent, Fold, Draggable, foldAll, Check } from 'he-tree-vue'

    // @ts-ignore <---- does not help
    const HeTree = HeTreeComponent.mixPlugins([ Fold, Draggable, foldAll, Check ])

    // TS ERROR: 'HeTreeComponent' only refers to a type, but is being used as a value here.ts(2693)

    // TS ERROR: 'Fold' only refers to a type, but is being used as a value here.ts(2693)
</script>

Here is my configuration in the tsconfig file:

{
    "compilerOptions": {
        "target": "esnext",
        "baseUrl": "./src/",
        "useDefineForClassFields": true,
        "module": "esnext",
        "moduleResolution": "node",
        "strict": true,
        "strictNullChecks": true,
        "forceConsistentCasingInFileNames": true,
        "useUnknownInCatchVariables": false,
        "jsx": "preserve",
        "paths": {
            "@/*": ["./*"],
        },
        "sourceMap": true,
        "resolveJsonModule": true,
        "esModuleInterop": true,
        "lib": ["esnext", "dom"],
        "isolatedModules": false,
        "skipDefaultLibCheck": true,
        "skipLibCheck": true,
        "strictFunctionTypes": true,
        "allowJs": true,
    },

    "include": [
        "src/**/*.ts",
        "src/**/*.d.ts",
        "src/**/*.tsx",
        "src/**/*.vue",
        "components.d.ts"
    ]
}

Answer №1

I was able to resolve the issue by setting up a new environment where @ts-ignore functions correctly.

<script setup lang="ts">
    import { Tree as HeTreeComponent, Fold, Draggable, foldAll, Check } from 'he-tree-vue'

    // Establishing a fresh context to bypass a TypeScript error related to the he-tree-vue library.
    const HeTree: HeTreeComponent = (() => {
        // @ts-ignore
        return HeTreeComponent.mixPlugins([ Fold, Draggable, foldAll, Check ])
    })()
</script>

Answer №2

It appears that the he-tree-vue package is defining Tree differently in two separate places. In one instance, it is defined as a type here, and in another instance, it is defined as an object here.

However, the README suggests checking out the new version he-tree, which includes virtualization list features and is Typescript friendly. You may want to take that into consideration. (https://github.com/phphe/he-tree)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

To work on React with typescript, your Type must include a method '[Symbol.iterator]()' that will fetch an iterator

Here is a demonstration I have prepared for you: https://stackblitz.com/edit/react-ts-shopping-cart-ssofgc?file=Shop.tsx Apologies for the lack of clarity in my question, but... The demo I created showcases a basic shopping cart using React and Typescri ...

Troubles with Jest tests are encountered when using ts-jest in an ES2020/ESNEXT TypeScript project

Currently, I am working on a VueJS project that utilizes ViteJS for transpilation, which is functioning properly. However, when Jest testing is involved alongside ts-jest, the following Jest configuration is used: jest.config.ts import { resolve } from &q ...

Sharing Properties Across Angular Components using TypeScript

Seeking a solution for storing properties needed in multiple components using a config file. For example: number-one-component.ts export class NumberOneComponent { view: any[]; xAxis: number; yAxis: number; label: string; labelPositio ...

When tests/** are not included in the tsconfig, the TS language features in Vscode become inaccessible

I am looking to configure my TypeScript tests in such a way that they receive linting, code completion, and VSCode intellisense (TypeScript language features) when the test folder is placed next to the src folder. However, I want to ensure that my tests do ...

Multiple components are returned with switch case

I am trying to iterate over an object and display a result based on Object.entries. However, the loop currently stops at the first return statement. Is there a way for me to capture and display all components returned simultaneously, perhaps using a vari ...

Updating the FormControl value using the ControlValueAccessor

I have developed a directive specifically for case manipulation. The code I created successfully updates the visual value using _Renderer2, but I am facing an issue with formGroup.value. Even though the directive visually changes the value as expected, t ...

Angular/Typescript code not functioning properly due to faulty expressions

What could be causing my {{ expression }} to malfunction? I have exhausted all options, yet the web browser fails to recognize this {{ expression }} or properly bind it using ng-bind. Instead, it either displays the {{ expression }} as is or not at all. C ...

Display a modal dialog using HttpInterceptor

@Injectable() export class MyInterceptor implements HttpInterceptor { intercept(req : HttpRequest<any>, next : HttpHandler) : Observable<HttpEvent<any>> { //display a modal dialog to wait for user response before proceeding with t ...

Cypress - Adjusting preset does not impact viewportHeight or Width measurements

Today is my first day using cypress and I encountered a scenario where I need to test the display of a simple element on mobile, tablet, or desktop. I tried changing the viewport with a method that seems to work, but unfortunately, the config doesn't ...

CompositeAPI: Referencing HTML Object Template - Error TS2339 and TS2533 when using .value to access Proxy Object

Having trouble referencing an element in VueJS 3 CompositeAPI. In my current implementation, it looks like this: <div ref="myIdentifier"></div> setup() { const myIdentifier = ref(null); onMounted(() => { console.log(myIden ...

The object may be null even after being enclosed in an if statement

In my Vue component, I have implemented the following method: dataURLtoBlob(dataurl: string): Blob { const arr: string[] = dataurl.split(","); if (arr) { if (arr[0]) { const mime = arr[0].match(/:(.*?);/)[1]; ...

Setting up Stylelint in a Vue 3 app with VSCode to automatically lint on save

I am looking to perform linting on my scss files and scss scope within .vue components. Here is what my configuration looks like in stylelint.config: module.exports = { extends: [ 'stylelint-config-standard', 'stylelint-config-rece ...

A foundational NodeJS program in TypeScript featuring a structured client-utility-definition setup with adherence to stringent coding guidelines

What is the best way to set up a basic TypeScript framework for starting a program with strict settings, based on the following program structure? An initial "client" code containing the actual program logic A separate "utility" module for defining funct ...

Executing asynchronous methods in a Playwright implementation: A guide on constructor assignment

My current implementation uses a Page Object Model to handle browser specification. However, I encountered an issue where assigning a new context from the browser and then assigning it to the page does not work as expected due to the asynchronous nature of ...

NextJS: Error - Unable to locate module 'fs'

Attempting to load Markdown files stored in the /legal directory, I am utilizing this code. Since loading these files requires server-side processing, I have implemented getStaticProps. Based on my research, this is where I should be able to utilize fs. Ho ...

AOT compile error due to Angular interpolation syntax

I am facing an issue while compiling my application. The AOT compiler is showing an error related to Angular interpolation in an Angular 2 form: Property 'address' does not exist on type 'FirebaseObjectObservable'. Here's a sn ...

Angular 4 allows for dynamically applying the active class to a clicked button, enhancing interactivity

Issue: <button *ngFor="let button of buttons" [ngClass]="{'active': isClicked}" (click)="isClicked = !isClicked" Description: A total of 10 buttons are displayed on the screen. When I click on button number 1, each button receives the clas ...

Interpolation is not available for my Angular application

I am having trouble with using interpolation on my input in this code. I tried setting the value of the input to be the same as the User's username, but it doesn't seem to work. <ion-header> <ion-toolbar> <ion-buttons slot=&q ...

The module named "mongoose" does not have any member called 'PaginateResult' exported

I'm facing an issue while trying to add the necessary types for "mongoose-paginate" in my Angular 4 project setup with "angular-cli". The problem arises when Webpack throws an error. import {PaginateResult} from "mongoose"; ... getAll(page: number) ...

What is the abbreviation for indicating a return type as nullable?

Is there a way to use shorthand for nullable return types in TypeScript similar to using "name?: type" for parameters? function veryUsefulFunction(val?: string /* this is OK */) // but not this or other things I've tried. // is there a way a ...