typescript + type definition file (.d.ts) + enumeration (Enum)

Asked

Viewed 543 times

1

I am trying to create a definition file that uses an enumerator, defined in another file, and a declared variable (declare var) in the.ts file, but when referencing this file in another implementation file, it informs that the variable does not exist.

Removing the import and declaring the enumerator in the .d.ts file the variable vmMensage normally recognizes, but error occurs that the enumerator is not the same as the type.Enum file.

If the way I did it doesn’t work, how can I declare an enumerator so I can reuse it at various project locations?

type.enumts.

export enum TipoMensagem {
    INFO = 0, ALERT = 1, ERROR = 2, OK = 3
}

export enum FiltroLogica {
    E = 1, OU = 2
}      

vm.mensagem.d.ts

/// <reference path="../../knockout/knockout.d.ts" />

import Enums = require("app/types/type.enum");

interface Mensagem {
    tipo: Enums.TipoMensagem;
    texto: string;
}

interface MensagemVM {
    listaMensagem: KnockoutObservableArray<Mensagem>;
    ExibirMensagem(tipo: Enums.TipoMensagem, texto: string): void;
    FadeIn(elem, index, item): void;
}

declare var vmMensagem: MensagemVM;

type.mensagemts.

/// <reference path="../../typings/vms/common/vm.mensagem.d.ts" />

import Enums = require("app/types/type.enum");

class Mensagem {

    tipo: Enums.TipoMensagem;
    texto: string;

    public constructor(tipo: Enums.TipoMensagem, txt: string) {
        this.tipo = tipo;
        this.texto = txt;
    }

    //Exibe a mensagem no elemento informado
    public SetElement(htmlElem: HTMLElement): void {
        $(htmlElem).delay(5000).fadeOut(600, () => {
            if (vmMensagem.listaMensagem.length == 0)
                return;

            $(this).remove();
            vmMensagem.listaMensagem.remove(this);
        });
    }
} 

export = Mensagem;

In type.mensagemts. in function public Setelement(htmlElem: Htmlelement): void the build error occurs Could not find Symbol 'vmMensagem'.

1 answer

2

I managed to solve my problem.

I removed the file type.enumts. and created another definition (Enum.d.ts) containing the Numerators, referencing it in the necessary files, as it is not necessary to import the file containing the Numerators in which the . js generated already has the values applied directly, serving only as a reference in the development.

Enum.d.ts

declare module Enums {
    enum TipoMensagem {
        INFO = 0, ALERT = 1, ERROR = 2, OK = 3
    }

    enum FiltroLogica {
        E = 1, OU = 2
    }
} 

Browser other questions tagged

You are not signed in. Login or sign up in order to post.