Open emmiep opened 8 months ago
I'm not sure why the Models.Document
type needs the index signature, is this a thing to make sure people who use JS/don't use the type parameter don't get any warnings when accessing custom attributes from the documents?
Personally, I want the type safety of Typescript, so I would prefer if the Document
base type didn't include that.
Is it possible to remove this from the Document
, then maybe creating an AnyDocument
type which could be the default type parameter value?
/* models.ts */
export type Document = {
$id: string;
$collectionId: string;
$databaseId: string;
$createdAt: string;
$updatedAt: string;
$permissions: string[];
};
export interface AnyDocument extends Document {
[key: string]: any;
}
/* databases.ts */
async createDocument<Document extends Models.Document = Models.AnyDocument>(
databaseId: string,
collectionId: string,
documentId: string,
data: Omit<Document, keyof Models.Document>,
permissions?: string[],
): Promise<Document> {
/* ... */
}
That would probably solve this issue as well.
@emmiep,
I'm not sure why the
Models.Document
type needs the index signature, is this a thing to make sure people who use JS/don't use the type parameter don't get any warnings when accessing custom attributes from the documents?
Correct, this is to prevent errors when someone just calls createDocument()
without passing in their own type.
Personally, I want the type safety of Typescript, so I would prefer if the
Document
base type didn't include that. Is it possible to remove this from theDocument
, then maybe creating anAnyDocument
type which could be the default type parameter value?/* models.ts */ export type Document = { $id: string; $collectionId: string; $databaseId: string; $createdAt: string; $updatedAt: string; $permissions: string[]; }; export interface AnyDocument extends Document { [key: string]: any; } /* databases.ts */ async createDocument<Document extends Models.Document = Models.AnyDocument>( databaseId: string, collectionId: string, documentId: string, data: Omit<Document, keyof Models.Document>, permissions?: string[], ): Promise<Document> { /* ... */ }
This is a great idea! We generate our SDKs using our sdk-generator and the template for the [key: string]: any;
is here. The tough thing would be to figure out how to update the SDK generator to support what you're suggesting π§
Opening this for contributors to see if they can update the SDK generator to add this behavior.
π Reproduction steps
When using an IDE/editor supporting autocompletion with the Typescript Language Server, such as VS Code.
Calling
Databases.createDocument
with a type parameter:π Expected behavior
I should see the properties I defined in the
ProjectModel
interface when VS Code autocompletes the code inside the curly brackets.π Actual Behavior
VS Code shows identifiers from the function scope, but not the properties defined in the
ProjectModel
interface:Possible solution
I think this is caused by the
data
argument ofDatabases.createDocument
having the typeOmit<Document, keyof Models.Document>
.Since
Models.Document
contains a string index signature, the value ofkeyof Models.Document
is probablystring | number
. That would remove all properties from theDocument
type parameter, basically turning it into an empty object type ({}
).π² Appwrite version
Different version (specify in environment)
π» Operating system
MacOS
𧱠Your Environment
Appwrite Cloud
Appwrite web SDK version:
VS Code version information:
MacOS version:
π Have you spent some time to check if this issue has been raised before?
π’ Have you read the Code of Conduct?