1

Luxon date library has a DateTime type. Instead of importing DateTime directly from luxon library like import { DateTime } from 'luxon'; I would like to extend the library to use a custom type that can be used through out in the project components and this would enable us to replace the third party library in one place in the future with another if needed.

First question: Is there any easy way to do this without custom extending the library? if not then would it be possible to use the same name? (AFAIK no)

export interface {CustomPrefixName}DateTime extends DateTime {

}

This works but any way to get rid of prefix name?

then then import custom datetime in components

import { CustomDateTime } from 'somewhereInProject';

akd
  • 6,538
  • 16
  • 70
  • 112
  • You should be able to simply make a new file, your class or interface named DateTime in any directory of your project. Import the luxon library into that file, then import your newly created file with the DateTime name into whichever location you want, but use the new import. The import name is dependent on where your file is located, since namespaces aren't really used in TypeScript. Then, if you ever change the base library all you have to do is change the import location in your custom DateTime class file, provided the same methods and properties exist. – HyperTextCoffeePot Dec 10 '20 at 14:37
  • you saying `export interface DateTime extends DateTime { }` is allowed? – akd Dec 10 '20 at 14:40
  • Oh, yeah I think you're right, there will be a name clash. Does this link help? [Import two classes with the same name](https://stackoverflow.com/questions/39610294/import-two-exported-classes-with-the-same-name) So it would be: import {CustomDateTime as DateTime} from '../myCustomDateTimeClass' – HyperTextCoffeePot Dec 10 '20 at 14:42

1 Answers1

1

this works

import {DateTime as LuxonDateTime} from 'luxon'

export interface DateTime extends LuxonDateTime {
    
}
akd
  • 6,538
  • 16
  • 70
  • 112