Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mandatory String type is hard to manage with typescript #16

Open
neilime opened this issue Nov 15, 2018 · 3 comments
Open

Mandatory String type is hard to manage with typescript #16

neilime opened this issue Nov 15, 2018 · 3 comments

Comments

@neilime
Copy link

neilime commented Nov 15, 2018

Hi,

I have an issue with your plugin, used with typescript.
Typing a property as String is mandatory and your plugin throws an exception if not.
However, your plugin transform the property that is not a string, but an object like :

{ en: 'string', fr: 'string' }

The typescript definition should be something like that :

IntlString = {
    [key: string]: String;
};

Can you provide these interface and allows it as property type, it may be helps us to fix this related issue : szokodiakos/typegoose#85

Thanks

@kalicki
Copy link

kalicki commented Jan 29, 2019

Check my module for this... @neilime

/// <reference types="mongoose" />

declare module 'mongoose' {
  import mongoose = require('mongoose');
  import mongooseIntl = require('mongoose-intl');

  // methods
  export interface MongooseIntlDocument extends Document {
    getLanguages(): [string];
    getLanguage(): string;
    setLanguage(lang: string): void;
    unsetLanguage(): void;
  }

  // statics
  interface MongooseIntlModel<T extends Document> extends Model<T> {
    getLanguages(): [string];
    getDefaultLanguage(): string;
    setDefaultLanguage(lang: string): void;
  }

  interface mongoose extends NodeJS.EventEmitter, ModelProperties {
    getLanguages(): [string];
    getDefaultLanguage(): string;
    setDefaultLanguage(lang: string): void;
  }

  export interface MongooseIntlOptions {
    languages: [string];
    defaultLanguage: string;
  }

  export interface MongooseIntlSchema extends Schema {
    plugin(
      plugin: (
        schema: MongooseIntlSchema,
        options: MongooseIntlOptions
      ) => void,
      options: MongooseIntlOptions
    ): this;

    // overload for the default mongoose plugin function
    plugin(
      plugin: (schema: Schema, options: Object) => void,
      opts?: Object
    ): this;
  }

  export function model(
    name: string,
    schema?: Schema,
    collection?: string,
    skipIntl?: boolean
  ): MongooseIntlModel<any>;

  export function setDefaultLanguage(lang: string): typeof mongoose;
}

declare module 'mongoose-intl' {
  import mongoose = require('mongoose');
  var _: (schema: mongoose.Schema, options: Object) => void;
  export = _;
}

@manishoo
Copy link

manishoo commented Aug 1, 2019

@kalicki How do I use that code?

@shahidcodes
Copy link
Contributor

@manishoo put the above module declaration in global.d.ts. Typescript will automatically pick it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants