Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extend interface defined in .d.ts file

In my TypeScript project, I use DefinitelyTyped definitions for external js dependencies.

Sometimes it might happen that these definitions are outdated. It might also happen than some libraries can add new methods at runtime, like express-validator in which you can define custom validator functions.

Therefore I would like to extend those .d.ts definitions adding new methods and/or properties.

So if I have my DefinitelyTyped defininiton in express-validator.d.ts:

declare module ExpressValidator {   export interface Validator {     is(): Validator;     not(): Validator;     isEmail(): Validator;     ...   } } 

how can I extend Validator interface within, for example, my application.ts ?

///<reference path='../typings/tsd.d.ts' />  import expressValidator = require('express-validator'); export var app = express();  app.use(expressValidator({     customValidators: {         isArray: function(value) {             return Array.isArray(value);         }  } }));  // How to extend Validator interface adding isArray() method?? 
like image 222
Marco Ancona Avatar asked Oct 05 '15 12:10

Marco Ancona


People also ask

How do you extend an interface in TypeScript?

Extending Interfaces in TypeScript # Use the extends keyword to extend interfaces in TypeScript, e.g. interface Dog extends Animal {age: number;} . The extends keyword allows us to copy the members from other named types and add new members to the final, more generic interface. Copied!

What is Extension D ts?

d stands for Declaration Files: When a TypeScript script gets compiled there is an option to generate a declaration file (with the extension . d. ts) that functions as an interface to the components in the compiled JavaScript.

Can an interface extend a class TypeScript?

In TypeScript, interfaces can also extend classes, but only in a way that involves inheritance. When an interface extends a class, the interface includes all class members (public and private), but without the class' implementations.

Can interface extend two interfaces TypeScript?

You can extend from as many interfaces as necessary by separating the interfaces with a comma. You are not required to add any new members to the final interface and can use the extends keyword to simply combine interfaces.


2 Answers

// How to extend Validator interface adding isArray() method??

You cannot do this in a file that is a module (some guidance here) and your file is a module because you have import expressValidator.

Instead create a extendedValidator.d.ts and add the new stuff for TypeScript's engine:

declare module ExpressValidator {   export interface Validator {      isArray: any;   } } 
like image 189
basarat Avatar answered Sep 28 '22 12:09

basarat


I was able to accomplish this by following the directions at https://www.credera.com/blog/technology-solutions/typescript-adding-custom-type-definitions-for-existing-libraries/. There they use the example of react.

import 'react';   declare module 'react' {     interface OlHTMLAttributes<T> {         type?: "1" | "a" | "A" | "i" | "I";     } } 

I found this works really well for winston too.

like image 39
cjbarth Avatar answered Sep 28 '22 11:09

cjbarth