V
V
Vasily Vasilyev2021-08-24 08:10:29
typescript
Vasily Vasilyev, 2021-08-24 08:10:29

How to add a new property to the types of the included library?

Connected to the firebase project, added

firebase.getCurrentUser = () => new Promise((resolve, reject) => {
  const unsubscribe = firebase.auth().onAuthStateChanged((user) => {
    unsubscribe();
    resolve(user);
  }, reject);
});

How to typecheck a new property?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Belyaev, 2021-08-24
@bingo347

https://www.typescriptlang.org/docs/handbook/decla...
Create a .d.ts file at the root with a name that doesn't match other files (so-called free declaration) and expand the namespace:

import 'firebase'; // нужно, чтоб не перетирать модуль, а мержить
declare module 'firebase' {
  namespace firebase {
    export const getCurrentUser: () => Promise<User>; // User тоже надо заимпортить
  }
}

A
Alexandroppolus, 2021-08-24
@Alexandroppolus

Basically a bad idea. First, it is not necessary to reforge a completely specific module into a "divine object" (see patterns, anti-patterns, SRP, etc.). Secondly, 'firebase' has its own documentation, and suddenly a method appears bypassing this documentation, the project will become more difficult for other developers to understand - even if they suddenly get around to describing their inserts, the documentation will turn out to be scattered across resources. Thirdly, the library may have its own method with the same name, and this is the most difficult case, there is no need to even explain here.
Maybe there are some more points, but the text field is too narrow to write from here..

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question