chore: begins ts improvements
This commit is contained in:
@@ -23,7 +23,7 @@ export type Arguments = {
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
async function findByID<T extends TypeWithID = any>(incomingArgs: Arguments): Promise<T> {
|
||||
async function findByID<T>(incomingArgs: Arguments): Promise<T> {
|
||||
let args = incomingArgs;
|
||||
|
||||
// /////////////////////////////////////
|
||||
|
||||
@@ -1,5 +1,4 @@
|
||||
import { BaseConfig } from '../../../config/types';
|
||||
import { TypeWithID } from '../../config/types';
|
||||
import { PayloadRequest } from '../../../express/types';
|
||||
import { Document } from '../../../types';
|
||||
import findByID from '../findByID';
|
||||
@@ -7,8 +6,8 @@ import { Payload } from '../../../payload';
|
||||
import { getDataLoader } from '../../dataloader';
|
||||
import i18n from '../../../translations/init';
|
||||
|
||||
export type Options<Config extends BaseConfig = any> = {
|
||||
collection: keyof Config['collections']
|
||||
export type Options<T extends keyof BaseConfig['collections']> = {
|
||||
collection: T
|
||||
id: string
|
||||
depth?: number
|
||||
currentDepth?: number
|
||||
@@ -23,7 +22,7 @@ export type Options<Config extends BaseConfig = any> = {
|
||||
}
|
||||
|
||||
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
||||
export default async function findByIDLocal<T extends TypeWithID = any>(payload: Payload, options: Options): Promise<T> {
|
||||
export default async function findByIDLocal<T extends keyof BaseConfig['collections']>(payload: Payload<BaseConfig>, options: Options<T>): Promise<BaseConfig['collections'][T]> {
|
||||
const {
|
||||
collection: collectionSlug,
|
||||
depth,
|
||||
@@ -53,7 +52,7 @@ export default async function findByIDLocal<T extends TypeWithID = any>(payload:
|
||||
if (!req.t) req.t = req.i18n.t;
|
||||
if (!req.payloadDataLoader) req.payloadDataLoader = getDataLoader(req);
|
||||
|
||||
return findByID({
|
||||
return findByID<T>({
|
||||
depth,
|
||||
currentDepth,
|
||||
id,
|
||||
|
||||
@@ -6,11 +6,12 @@ import { PayloadRequest } from '../../../express/types';
|
||||
import { getDataLoader } from '../../dataloader';
|
||||
import { File } from '../../../uploads/types';
|
||||
import i18nInit from '../../../translations/init';
|
||||
import { BaseConfig } from '../../../config/types';
|
||||
|
||||
export type Options<T> = {
|
||||
collection: string
|
||||
export type Options<Config extends BaseConfig, Slug extends keyof BaseConfig['collections']> = {
|
||||
collection: Slug
|
||||
id: string | number
|
||||
data: Partial<T>
|
||||
data: Config['collections'][Slug]
|
||||
depth?: number
|
||||
locale?: string
|
||||
fallbackLocale?: string
|
||||
@@ -24,7 +25,7 @@ export type Options<T> = {
|
||||
autosave?: boolean
|
||||
}
|
||||
|
||||
export default async function updateLocal<T = any>(payload: Payload, options: Options<T>): Promise<T> {
|
||||
export default async function updateLocal<Config extends BaseConfig, Slug extends keyof BaseConfig['collections']>(payload: Payload<BaseConfig>, options: Options<Config, Slug>): Promise<Config['collections'][Slug]> {
|
||||
const {
|
||||
collection: collectionSlug,
|
||||
depth,
|
||||
@@ -75,5 +76,5 @@ export default async function updateLocal<T = any>(payload: Payload, options: Op
|
||||
req,
|
||||
};
|
||||
|
||||
return update(args);
|
||||
return update<Config, Slug>(args);
|
||||
}
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import httpStatus from 'http-status';
|
||||
import { BaseConfig } from '../../config/types';
|
||||
import { Where, Document } from '../../types';
|
||||
import { Collection } from '../config/types';
|
||||
import sanitizeInternalFields from '../../utilities/sanitizeInternalFields';
|
||||
@@ -18,11 +19,11 @@ import { afterRead } from '../../fields/hooks/afterRead';
|
||||
import { generateFileData } from '../../uploads/generateFileData';
|
||||
import { getLatestCollectionVersion } from '../../versions/getLatestCollectionVersion';
|
||||
|
||||
export type Arguments = {
|
||||
export type Arguments<T extends Record<string, unknown>> = {
|
||||
collection: Collection
|
||||
req: PayloadRequest
|
||||
id: string | number
|
||||
data: Record<string, unknown>
|
||||
data: T
|
||||
depth?: number
|
||||
disableVerificationEmail?: boolean
|
||||
overrideAccess?: boolean
|
||||
@@ -32,7 +33,9 @@ export type Arguments = {
|
||||
autosave?: boolean
|
||||
}
|
||||
|
||||
async function update(incomingArgs: Arguments): Promise<Document> {
|
||||
async function update<Config extends BaseConfig, Slug extends keyof BaseConfig['collections']>(
|
||||
incomingArgs: Arguments<Config['collections'][Slug]>,
|
||||
): Promise<Config['collections'][Slug]> {
|
||||
let args = incomingArgs;
|
||||
|
||||
// /////////////////////////////////////
|
||||
|
||||
@@ -22,10 +22,10 @@ import { User } from '../auth/types';
|
||||
|
||||
export interface BaseConfig {
|
||||
collections: {
|
||||
[slug: string]: Record<string, unknown>
|
||||
[slug: string | number | symbol]: Record<string, unknown>
|
||||
}
|
||||
globals: {
|
||||
[slug: string]: Record<string, unknown>
|
||||
[slug: string | number | symbol]: Record<string, unknown>
|
||||
}
|
||||
}
|
||||
|
||||
@@ -109,7 +109,7 @@ export type InitOptions = {
|
||||
/**
|
||||
* A function that is called immediately following startup that receives the Payload instance as it's only argument.
|
||||
*/
|
||||
onInit?: (payload: Payload) => Promise<void> | void;
|
||||
onInit?: <C extends BaseConfig = any>(payload: Payload<C>) => Promise<void> | void;
|
||||
|
||||
/**
|
||||
* Specify options for the built-in Pino logger that Payload uses for internal logging.
|
||||
@@ -541,7 +541,7 @@ export type Config = {
|
||||
/** Send anonymous telemetry data about general usage. */
|
||||
telemetry?: boolean;
|
||||
/** A function that is called immediately following startup that receives the Payload instance as its only argument. */
|
||||
onInit?: (payload: Payload) => Promise<void> | void;
|
||||
onInit?: <C extends BaseConfig = any>(payload: Payload<C>) => Promise<void> | void;
|
||||
};
|
||||
|
||||
export type SanitizedConfig = Omit<
|
||||
|
||||
@@ -70,7 +70,7 @@ export class Payload<Config extends BaseConfig = any> {
|
||||
config: SanitizedConfig;
|
||||
|
||||
collections: {
|
||||
[slug: string]: Collection;
|
||||
[slug: string | number | symbol]: Collection;
|
||||
} = {}
|
||||
|
||||
versions: {
|
||||
@@ -272,9 +272,10 @@ export class Payload<Config extends BaseConfig = any> {
|
||||
* @param options
|
||||
* @returns document with specified ID
|
||||
*/
|
||||
findByID = async (options: FindByIDOptions<Config>): Promise<Config['collections'][ValueOf<typeof options['collection']>]> => {
|
||||
|
||||
findByID = async <T extends keyof Config['collections']>(options: FindByIDOptions<T>): Promise<Config['collections'][T]> => {
|
||||
const { findByID } = localOperations;
|
||||
return findByID(this, options);
|
||||
return findByID<T>(this, options);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -282,9 +283,9 @@ export class Payload<Config extends BaseConfig = any> {
|
||||
* @param options
|
||||
* @returns Updated document
|
||||
*/
|
||||
update = async <T = any>(options: UpdateOptions<T>): Promise<T> => {
|
||||
update = async <T extends keyof Config['collections']>(options: UpdateOptions<Config, T>): Promise<Config['collections'][T]> => {
|
||||
const { update } = localOperations;
|
||||
return update<T>(this, options);
|
||||
return update<Config, T>(this, options);
|
||||
}
|
||||
|
||||
delete = async <T extends TypeWithID = any>(options: DeleteOptions): Promise<T> => {
|
||||
@@ -355,7 +356,7 @@ if (!cached) {
|
||||
cached = global.payload = { payload: null, promise: null };
|
||||
}
|
||||
|
||||
export const getPayload = async <T>(options: InitOptions): Promise<Payload<T>> => {
|
||||
export const getPayload = async <T extends BaseConfig>(options: InitOptions): Promise<Payload<T>> => {
|
||||
if (cached.payload) {
|
||||
return cached.payload;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user