Any support for TypeScript?

Hey :slight_smile:

Simple question: do you have any support for full typescript approach? Or do you support it already? (I am way more a typescript guy than a javascript dude…

Thanks!

Max

1 Like

Hi again :smiley:

To make it easy for you, we created our own typings to help you using our exposed tools in TypeScript. You’ll be able to add static types in your code in no time.

You can follow our guide here to Migrate your project to TypeScript :gear:

1 Like

Hey !

Your link helped me, thank you for that :slight_smile:

As I pointed out in another “topic”, I am not at all a NodeJs developer which makes things a bit hard for me (former C, C++, C#, Go developer). Typescript makes definetely things easy for me and so, I am trying to fully convert the project into staging.

What I did yet

I could transform into typescript the followings:

config/
constants/
forest/
models/
routes/
storage/
utils/

However, there is still some of them that I haven’t converted. I tried to transform middlewares/forestadmin.js but I couldn’t make it as Liana.init(...) had an error as the function init(...) doesn’t exist… There is the list (I hope you can help me or maybe provide an extension of the documentation present in your link above):

middlewares/
app.js
server.js

These four above files are quite complicated to transform into typescript (for me) but I will try to move forward anyway and keep you update.

Some others related questions

  1. Nested models

I have a model where you have the following:

models/notes.ts

Important: This model is only used as nested, it doesn’t have dedicated collection unlike Item

// This model was generated by Lumber. However, you remain in control of your models.
// Learn how here: https://docs.forestadmin.com/documentation/v/v6/reference-guide/models/enrich-your-models
import { Schema, Document, model} from 'mongoose';

interface INote extends Document {
  title: string,
  message: string,
}

// This section contains the properties of your model, mapped to your collection's properties.
// Learn more here: https://docs.forestadmin.com/documentation/v/v6/reference-guide/models/enrich-your-models#declaring-a-new-field-in-a-model
const schema = new Schema({
  "title": String,
  "message": String,
}, {
  timestamps: false,
});

export default model<INote>('notes', schema);

models/items.ts

import { Schema, Document, model} from 'mongoose';
import notes from './notes.ts';

interface IItem extends Document {
  notes: [notes],
  // ...
}

// This section contains the properties of your model, mapped to your collection's properties.
// Learn more here: https://docs.forestadmin.com/documentation/v/v6/reference-guide/models/enrich-your-models#declaring-a-new-field-in-a-model
const schema = new Schema({
  'notes': [notes],
  // ...
}, {
  timestamps: false,
});

export default model<IItem>('items', schema, 'Items');

The thing is that I can’t do that and I don’t understand why. The error message says that I use a value as type… can you explain? Thanks

  1. Models transformed but still unable to access member (Interface approache)

The below code is related to another question of mine on this community website but I was hoping that translating the JS into TS would help and… I couldn’t make it work as expected. I have the following piece of code:

// ...
import items from '../models/items';

// ...

// Upload image to gcs
router.post('/actions/items/upload-item-image', permissionMiddlewareCreator.smartAction(), (req, res) => {
    // ...
    const recordGetter = new RecordGetter(items);
    recordGetter.get(itemID)
        .then((item: typeof items) => {
           // We do agree that i should be able to access the properties of the schema or the interface.. but still I have to access them like this, why?
            let itemNotes = item["notes"]
            // ...
        })
});

Note: The above used model is defined in within this comment. Check the question 1 just above this one (currently question 2)

Thanks for your help!

PS: We can generate a full forest admin project using either SQL or MongoDB, either Docker or Npm, so why couldn’t we have the choice between JavaScript and Typescript? Feature suggestions

Max

Hey!

As I moved forward, I wanted to update you :slight_smile:

So, if I export IItem (my interface) and cast the type, it does work as expected for the point 2: .then((item: IItem) =>

For the first point, nested model, i really don’t get it tho :confused: I still have it declared as

import { Schema, Document, model} from 'mongoose';
import notes from './notes.ts';

interface IItem extends Document {
  notes: [INote], // same here, is this the way to go?
  // ...
}

// This section contains the properties of your model, mapped to your collection's properties.
// Learn more here: https://docs.forestadmin.com/documentation/v/v6/reference-guide/models/enrich-your-models#declaring-a-new-field-in-a-model
const schema = new Schema({
  'notes': [{
    title: String,
    message: String,
    _id: false,
  }]
  // ...
}, {
  timestamps: false,
});

export default model<IItem>('items', schema, 'Items');

If I’m not mistaking, I think there is a little error.
The function model<INote>('notes', schema) create a “collection” and you cannot create a schema depending on collection.
According to the documentation, I think you can try to simply export the schema.

export default new Schema({
  "title": String,
  "message": String,
}, {
  timestamps: false,
});

Let me know if it help.

Hey!

It did work :slight_smile:

So to add extra data to your answer:

  • Export the interface of INote (for uses in other interfaces)
  • Export as default the schema using the same approach as yours (for used in others schema)

In fact, the way I was doing was creating a collection, which as you pointed out, was wrong :slight_smile: so Thanks for this one :slight_smile:

Appreciate, thanks!

Max

Hi again,

I wanted to update you about the 4 files I still want to convert, I might have found 2 bugs or some missing information about it while reading your tutorial on "how to move to typescript with forest admin)

  • Regarding Liana (“forest-express-mongoose”) it actually (@types package) doesn’t export the init() part, might be a missing on your end?
  • When “starting” forest admin, a /dist folder get generated and everything works but one thing is missing still I converted to typescript → /view doesn’t seem to get copied and so the file is missing when trying to access it from the “welcome.js” file

Thanks :slight_smile:

I hope these two points can help both of us :slight_smile:

Max

Hey :slight_smile:

I wanted to know if you came again on this post and saw/read my last replies?
@anon20071947 @Arnaud_Moncel

Thanks,

Max