I am in the process of converting a Parse Server codebase from Javascript to Typescript – learning Typescript as I go. I would welcome any insight on best practices.
I’m wondering what the best way to approach strongly typing Parse Classes in my codebase? (are there any good reference Parse Server projects written in Typescript?)
Currently, I have a classes.ts
file in my root src
directory that holds all of my Class and Attribute definitions like so:
import { Attributes } from 'parse'
export interface ItemAttributes extends Attributes {
color: string
size: number
enabled: boolean
}
export class Item extends Object<ItemAttributes> {
constructor (data?: Partial<ItemAttributes>) {
super('Item', data as ItemAttributes)
}
}
Parse.Object.registerSubclass('Item', Item)
Has anyone found success with different folder structures than the above? It is getting a bit unwieldy with all of my classes in the one root file.
Also, I am looking to type the getter and setter arguments that currently exist as strings (for example: myItem.get('color')
).
I am looking for a solution that doesn’t require an additional enum definition. I find it tedious to have to maintain multiple definitions when updating class attributes.
export enum ItemKeys {
color = 'color'
size = 'size'
enabled = 'enabled'
}
// Usage:
myItem.get(ItemKeys.color)
Any feedback would be appreciated.