How to use modules and interfaces in TypeScript for NodeJS purpose?

My team and I are new to TypeScript and NodeJS, and we had serious problems getting this project off the ground. This meant being a modular game asset pipeline written in TypeScript for use with NodeJS (I have not been involved in decision making here, and the discussion is still incompatible, but we still need a prototype).

We already have a good architectural idea, but it's hard for me to even run a prototype, because I just can’t turn my head around how the modules work in conjunction with interfaces and external dependencies. Some of our components should also use NodeJS or other library modules (for example, NodeJS.fs for reading / writing files), and we would prefer to be able to use ///referenced.ts to add appropriate files to maintain type safety during development.

An example of the proposed project structure:

/pipeline
    /log
        - ILog.d.ts
        - StdOutLog.ts
        - FileLog.ts
        - ...
    /reader
        - IReader.d.ts
        - TextReader.ts
        - ...
    /parser
        - IParser.d.ts
        - XmlParser.ts
        - JsonParser.ts
        - ...
    /task
        - ITask.d.ts
        - JsTask.ts
        - CliTask.ts
        - ...
    /...

, , , ( , , , , , JSON XML,...) , (, class GitTask extends CliTask class JpgReader implements IReader).

project/runner :

import pipeline = require('pipeline');

//...

var log = new pipeline.log.FileLog();
log.info("filelog started");

var parser = new pipeline.parser.XmlParser();
parser.parse(somexmldata);
log.info("parsing XML");

// ...

, (tm), , TypeScript , , , , , (factory, logger, logitem, logtarget, loglevel). , NodeJS JS , import , , .

, TypeScript NodeJS? , ( FQN, , pipe.log.X, pipe.task.Y .., module export )? , ?


[] basarat, , . , :

error TS2095: Could not find symbol 'LogItem'.

LogItem - , /log StdOutLog.ts . IntelliJ (, LogItem, /log , ). log.LogItem, . . [/Update]

[2] , . / IntelliJ, grunt-ts.

//src/log/LogItem.ts:

///<reference path='../pipeline.d.ts'/>
class LogItem implements ILogItem {
    // ...

    constructor(level:LogLevel, message:string) {
        // ...
    }

    public static create(level:LogLevel, message:string):ILogItem {
        return new LogItem(level, message);
    }

    // ...
}

export = LogItem;

//src/log/Log.ts:

///<reference path='../pipeline.d.ts'/>
class Log implements ILog {
    // ...

    private getLogItem(level:LogLevel, message:string):ILogItem {
        return LogItem.create(level, message); // <-- that where I get the "symbol not found" error
    }

    // ...
}

export = Log;

//src/pipeline.d.ts:

///<reference path="../typings/node/node.d.ts" />

//grunt-start
/// <reference path="pipeline.ts" />
/// <reference path="log/Log.ts" />
/// <reference path="log/LogFactory.ts" />
/// <reference path="log/LogItem.ts" />
/// <reference path="log/StdLogEmitter.ts" />
/// <reference path="log/log.d.ts" />
/// <reference path="log/log.ts" />
/// <reference path="parser/JsonParser.ts" />
/// <reference path="parser/parser.d.ts" />
/// <reference path="parser/parser.ts" />
//grunt-end

[/2]

+4
1

.d.ts globals.d.ts, ///<reference + vendor.d.ts (, node.d.ts).

/pipeline
    /globals.d.ts
    /log
        - ILog.d.ts
        - StdOutLog.ts
        - FileLog.ts
        - ...
    /reader
        - IReader.d.ts
        - TextReader.ts
        - ...
    /parser
        - IParser.d.ts
        - XmlParser.ts
        - JsonParser.ts
        - ...
    /task
        - ITask.d.ts
        - JsTask.ts
        - CliTask.ts
        - ...
    /...

.d.ts.

typescript , . XmlParser.ts:

/// <reference path='../globals.d.ts'/>
class XMLParser implements IParser{
}
export = XMLParser;

index.ts, . :

export import XmlParser = require('./xmlParser'); 
// so on 

(pipeline/index.ts):

export import parser = require('./parser/index');

, pipe/index.ts, :

import pipeline = require('./pipeline/index');


var parser = new pipeline.parser.XmlParser();
parser.parse(somexmldata);
log.info("parsing XML");

. Grunt-ts import/export , : https://github.com/grunt-ts/grunt-ts/issues/85

+3

Source: https://habr.com/ru/post/1539885/


All Articles