Reputation: 655
Edited to try to be more specific (old example removed):
I would like to know how to reference types or interfaces defined in .d.ts files in my own classes and interfaces as types for function parameters or for class attributes.
Here is a example. I will explain some detail that is not completely relevant to the question but explains the code sample:
var fs = require("fs");
var path = require("path");
var Sequelize = require("sequelize");
var config = require('../config/environment');
class SequelizeDB{
sequelizeInstance: any;
sequelize: any;
modelDirectory: string;
constructor(Sequelize, dbConfig){
//instantiate the sequelize instance object
this.sequelize = Sequelize;
this.sequelizeInstance = this.getDatabaseInstance(this.sequelize,
dbConfig.databaseName,
dbConfig.userName,
dbConfig.password,
dbConfig.hostName,
dbConfig.port,
dbConfig.dialect);
this.modelDirectory = __dirname + '/sequelize';
this.importDataModels();
this.associateDataModels();
}
private getDatabaseInstance(Sequelize,
databaseName: string,
userName: string,
password: string,
hostName: string,
port: number,
dialect: string){
return new Sequelize(
databaseName,
userName,
password,
//options
{
host: hostName,
dialect: dialect,
port: port,
pool: {
max: 5,
min: 0,
idle: 10000
},
});
}
private importDataModels(){
//read in all the models from the current folder
fs
.readdirSync(this.modelDirectory)
.filter((fileName) => {
return this.isModelFile(fileName);
})
.forEach((file) =>{
//create the models in the seuelizeInstance
var model = this.sequelizeInstance.import(path.join(this.modelDirectory, file));
this[model.name] = model;
});
}
private isModelFile(fileName: string){
return (fileName.indexOf('.') !== 0) &&
(fileName !== 'index.js') &&
(fileName.indexOf('.map') === -1) && //ignore map files
(fileName.indexOf('.ts') === -1); //ignore ts files
}
private associateDataModels(){
Object.keys(this).forEach((modelName) => {
if (typeof( this[modelName]) === 'object' && 'associate' in this[modelName]) {
this[modelName].associate(this);
}
});
}
};
module.exports = new SequelizeDB(Sequelize, config.sql);
I am using Sequelize and I have a pattern set up for making the creation of the model easy as per their documentation. The current setup works for me and what the code is doing is creating a class that that when created sets up a database connection.
It then loops through all the files in the models folder and creates all the models using Seuquelize.import.
Now what I want to know is how I would be able to replace this part to use the actual types from the .d.ts file for sequelize (here)[https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/sequelize/sequelize.d.ts].
It appears to me there is a Sequelize static object that is used to create the (lower case) sequelize instance. Like so:
new Sequelize(//config values)
I want to store these two thing on the class I have created in the code above and currently to make them pass the compiler I have make them of type any
.
sequelizeInstance: any;
sequelize: any;
How would I be able to take advantage of the Sequelize .d.ts file that exists and be more explicit about the Type that I expect in that object and the type that is passed to its constructor.
This is not the only example but what I am more generally asking is I can create an interface or a class in TypeScript and inforce a function to take that particular class or an interface for example:
interface IUser{
id: number,
username: string,
passwork: string
}
function validateUser(user: IUser): boolean{
//implementation
}
How would I do that if the User interface was defined ina module in node that I installed using npm and have installed using tsd?
Please let me know if this is not enough detail and I will try to explain more.
Upvotes: 0
Views: 2229
Reputation: 7294
The easiest thing would be to use
/// <reference path="../typings/sequelize/sequelize.d.ts" />
And from that point you will be able to use the interfaces from that d.ts file. Also the compiler will know to include this file in the compilation, so no errors on the missing interfaces.
A most advanced option that you have is to use tsconfig.json (Most IDEs understand this file), and indicating all the files of your project (Including the .d.ts from tsd) inside the tsconfig.json and the compiler will "know" what to include in the compilation.
The best thing to do, is actually import the file like this:
import * from '../typings/sequelize/sequelize'; // without extension
And the compiler will fetch that file before it compiles your code. if it has only ambient declarations (d.ts) it will not include it in the final result (Which is good).
Upvotes: 3