common.ts 472 B

123456789101112131415161718192021
  1. /*
  2. * Common types and interfaces
  3. */
  4. import type { HasObjectId } from './has-object-id';
  5. // Foreign key field
  6. export type Ref<T> = string | T & HasObjectId;
  7. export type Nullable<T> = T | null | undefined;
  8. export const isPopulated = <T>(ref: T & HasObjectId | Ref<T>): ref is T & HasObjectId => {
  9. return !(typeof ref === 'string');
  10. };
  11. export const getIdForRef = <T>(ref: T & HasObjectId | Ref<T>): string => {
  12. return isPopulated(ref)
  13. ? ref._id
  14. : ref;
  15. };