1

I have defined a module like this in target.d.ts

declare module "target" {

    export interface Group {
        name: string;
        targets?: Target[];
    }

    export interface Target {
        device: Device;
        property: Property;
        value?: Value;
    }

    export interface Value {
        value: number;
        id?: number;
        timestamp?: number;
    }

    export interface Property {
        id: number;
        name: string;
        channel: number;
        type: string;
    }

    export interface Device {
        id: number;
        name: string;
        type: string;
    }

}

Now I am using the defined interfaces in a component like

import {Component, OnInit} from '@angular/core';

import {BackendUrl} from '../../../../common/settings';
import {contentHeaders} from "../../../../common/headers";
import {EventService} from "../../../../service/event.service";

import {Input} from "@angular/core/src/metadata/directives";
import {Message} from "stompjs";
import {Target} from "target";

@Component({
selector: 'device-switch',
templateUrl: './switch.component.html'
})
export class SwitchComponent implements OnInit {

    @Input()
    public title: string;

    @Input()
    public device: any;

    @Input()
    public property: any;

    private checked: boolean;

    constructor(public authHttp: AuthHttp, private eventService: EventService) {
        this.checked = false;
    }

    ngOnInit() {
        this.eventService.messages.subscribe(this.on_next);
    }

    /** Consume a message from the eventService */
    public on_next = (message: Message) => {

        let data : Target = JSON.parse(message.body);
        if(data.device.id === this.device.id && data.property.name === this.property.name) {
            this.checked = (data.value.value > 0);
        }

    };

}

The problem is that the @Input() variables device and property are interfaces types defined in my target module too.

But if I import {Target, Device, Property} from "target"; the compiler throws an error

ERROR in ./src/app/component/view/part/device/switch.component.ts
Module not found: Error: Can't resolve 'target' in '~/git/xx/frontend/src/app/component/view/part/device'
@ ./src/app/component/view/part/device/switch.component.ts 16:0-28
@ ./src/app/component/view/part/group.component.ts
@ ./src/app/app.module.ts
@ ./src/main.ts
@ multi main

I don't know whats the problem ... Intellij does not highlight anything ... seems to be ok.

2 Answers 2

3

The most likely cause of your error is that your definition file is not being included in the compilation context, so that your declare module 'target' is never called.

There are several ways you can chose to include it:

  1. Add ///<reference path="path/to/your/target.d.ts" /> to the top of your switch.component.ts.

  2. Add path/to/your/target.d.ts to the "file":[] array in your tsconfig.json

  3. Add path/to/your/target.d.ts (or an equivalent file pattern that would match it) to the "include":[] array in your tsconfig.json.

Once you get the target.d.ts included in your compilation context then you should be able to import {Target, Device, Property} from 'target' anywhere in your application.

Sign up to request clarification or add additional context in comments.

Comments

0

I came across this issue in an Angular 7.2.0 project, updating to 7.2.8 solved it.

In my case I needed to force the update and resolve a typescript dependency:

ng update --all --force

followed by

npm install --save typescript@">=3.1.1 <3.3.0"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.