Skip to content

Commit

Permalink
refactor: use TS noUncheckedIndexedAccess
Browse files Browse the repository at this point in the history
  • Loading branch information
aniravi24 committed Feb 18, 2025
1 parent 8e0353f commit 6fcbadf
Show file tree
Hide file tree
Showing 8 changed files with 23 additions and 36 deletions.
2 changes: 1 addition & 1 deletion packages/pg-migrate/src/migration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ const getLastSuffix = async (dir: string, ignorePattern?: string) => {
try {
const files = await loadMigrationFiles(dir, ignorePattern);
return files.length > 0
? getSuffixFromFileName(files[files.length - 1])
? getSuffixFromFileName(files[files.length - 1] as string)
: undefined;
} catch (err) {
return undefined;
Expand Down
2 changes: 1 addition & 1 deletion packages/pg-migrate/src/runner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ const checkOrder = (runNames: string[], migrations: Migration[]) => {
const len = Math.min(runNames.length, migrations.length);
for (let i = 0; i < len; i += 1) {
const runName = runNames[i];
const migrationName = migrations[i].name;
const migrationName = migrations[i]?.name;
if (runName !== migrationName) {
throw new Error(
`Not run migration ${migrationName} is preceding already run migration ${runName}`,
Expand Down
6 changes: 3 additions & 3 deletions packages/pg-migrate/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ export class StringIdGenerator {

private increment() {
for (let i = this.ids.length - 1; i >= 0; i -= 1) {
this.ids[i] += 1;
if (this.ids[i] < this.chars.length) {
this.ids[i] = (this.ids[i] ?? 0) + 1;
if ((this.ids[i] as number) < this.chars.length) {
return;
}
this.ids[i] = 0;
Expand Down Expand Up @@ -88,4 +88,4 @@ export const getSchemas = (schema?: string[] | string): string[] => {
export const getMigrationTableSchema = (options: RunnerOption): string =>
options.migrationsSchema !== undefined
? options.migrationsSchema
: getSchemas(options.schema)[0];
: (getSchemas(options.schema)[0] as string);
10 changes: 5 additions & 5 deletions packages/sidetrack/src/effect.ts
Original file line number Diff line number Diff line change
Expand Up @@ -335,7 +335,7 @@ export function layer<Queues extends SidetrackQueuesGenericType>(
message: (e as any).message,
}),
try: () =>
queues[job.queue].run(
queues[job.queue]!.run(
payloadDeserializer(job.queue, job.payload as Queues[string]),
{
job: job as SidetrackJob<Queues[string]>,
Expand Down Expand Up @@ -432,15 +432,15 @@ export function layer<Queues extends SidetrackQueuesGenericType>(
options?.uniqueKey,
],
),
).pipe(Effect.map((result) => result.rows[0]));
).pipe(Effect.map((result) => result.rows[0]!));

const getJob = (jobId: string, options?: SidetrackGetJobOptions) =>
Effect.promise(() =>
(options?.dbClient || dbClient).execute<SidetrackJobs>(
`SELECT * FROM sidetrack_jobs WHERE id = $1`,
[jobId],
),
).pipe(Effect.map((result) => result.rows[0]));
).pipe(Effect.map((result) => result.rows[0]!));

const scheduleCron = <K extends keyof Queues>(
queueName: K,
Expand All @@ -465,7 +465,7 @@ export function layer<Queues extends SidetrackQueuesGenericType>(
),
),
),
Effect.map((result) => result.rows[0]),
Effect.map((result) => result.rows[0]!),
Effect.tap((cronJob) => startCronJob(cronJob, options)),
);

Expand Down Expand Up @@ -579,7 +579,7 @@ export function layer<Queues extends SidetrackQueuesGenericType>(
)

.pipe(
Effect.map((result) => result.rows[0]),
Effect.map((result) => result.rows[0]!),
Effect.flatMap((job) => executeJobRunner(job, options)),
);

Expand Down
6 changes: 3 additions & 3 deletions packages/sidetrack/test/cron.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ describe("cron jobs", () => {
"SELECT status FROM sidetrack_cron_jobs WHERE queue = $1 AND cron_expression = $2",
["test", "*/5 * * * *"],
);
expect(result.rows[0].status).toBe("inactive");
expect(result.rows[0]?.status).toBe("inactive");
});
});

Expand Down Expand Up @@ -187,11 +187,11 @@ describe("cron jobs", () => {
expect(queue2Jobs.some((job) => job.status === "completed")).toBe(true);

// Verify payloads
expect(queue1Jobs[0].payload).toEqual({
expect(queue1Jobs[0]?.payload).toEqual({
description: "First cron job",
sequence: 1,
});
expect(queue2Jobs[0].payload).toEqual({
expect(queue2Jobs[0]?.payload).toEqual({
description: "Second cron job",
sequence: 2,
});
Expand Down
14 changes: 7 additions & 7 deletions packages/sidetrack/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -280,17 +280,17 @@ describe.concurrent("jobs", () => {
queue: ["scheduled"],
});
expect(jobsBeforeSchedule.length).toBe(1);
expect(jobsBeforeSchedule[0].status).toBe("scheduled");
expect(jobsBeforeSchedule[0].scheduled_at).toEqual(futureDate);
expect(jobsBeforeSchedule[0]?.status).toBe("scheduled");
expect(jobsBeforeSchedule[0]?.scheduled_at).toEqual(futureDate);

await sidetrack.runJobs({ queue: ["scheduled"] });

const jobsAfterSchedule = await sidetrack.listJobs({
queue: ["scheduled"],
});
expect(jobsAfterSchedule.length).toBe(1);
expect(jobsAfterSchedule[0].status).toBe("scheduled");
expect(jobsAfterSchedule[0].payload).toEqual({ message: "Future job" });
expect(jobsAfterSchedule[0]?.status).toBe("scheduled");
expect(jobsAfterSchedule[0]?.payload).toEqual({ message: "Future job" });

await sidetrack.runJobs({
includeFutureJobs: true,
Expand All @@ -301,8 +301,8 @@ describe.concurrent("jobs", () => {
queue: ["scheduled"],
});
expect(jobsAfterRun.length).toBe(1);
expect(jobsAfterRun[0].status).toBe("completed");
expect(jobsAfterRun[0].payload).toEqual({ message: "Future job" });
expect(jobsAfterRun[0]?.status).toBe("completed");
expect(jobsAfterRun[0]?.payload).toEqual({ message: "Future job" });
});
});

Expand Down Expand Up @@ -371,7 +371,7 @@ describe.concurrent("jobs", () => {

const jobs = await sidetrack.listJobs({ queue: ["test"] });
expect(jobs.length).toBe(1);
expect(jobs[0].payload).toEqual({ id: "suppress unique 1" });
expect(jobs[0]?.payload).toEqual({ id: "suppress unique 1" });
});
});

Expand Down
4 changes: 2 additions & 2 deletions packages/sidetrack/test/poll.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ describe("polling", () => {
const timestamps = [processedJob1, processedJob2, processedJob3].map(
(job) => job.attempted_at!.getTime(),
);
expect(timestamps[1] - timestamps[0]).toBeGreaterThanOrEqual(100); // At least one polling interval
expect(timestamps[2] - timestamps[1]).toBeGreaterThanOrEqual(100);
expect(timestamps[1]! - timestamps[0]!).toBeGreaterThanOrEqual(100); // At least one polling interval
expect(timestamps[2]! - timestamps[1]!).toBeGreaterThanOrEqual(100);
} finally {
// Clean up
await sidetrack.stop();
Expand Down
15 changes: 1 addition & 14 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,10 @@
"moduleResolution": "nodenext",
"noEmit": true, // TSUP takes care of emitting js for us, in a MUCH faster way
"noFallthroughCasesInSwitch": true,
// "noImplicitAny": true, /* Enable error reporting for expressions and declarations with an implied 'any' type. */
// "strictNullChecks": true, /* When type checking, take into account 'null' and 'undefined'. */
// "strictFunctionTypes": true, /* When assigning functions, check to ensure parameters and the return values are subtype-compatible. */
// "strictBindCallApply": true, /* Check that the arguments for 'bind', 'call', and 'apply' methods match the original function. */
// "strictPropertyInitialization": true, /* Check for class properties that are declared but not set in the constructor. */
// "noImplicitThis": true, /* Enable error reporting when 'this' is given the type 'any'. */
// "useUnknownInCatchVariables": true, /* Default catch clause variables as 'unknown' instead of 'any'. */
// "alwaysStrict": true, /* Ensure 'use strict' is always emitted. */
// "noUnusedLocals": true, /* Enable error reporting when local variables aren't read. */
// "noUnusedParameters": true, /* Raise an error when a function parameter isn't read. */
// "exactOptionalPropertyTypes": true, /* Interpret optional property types as written, rather than adding 'undefined'. */
// "noImplicitReturns": true, /* Enable error reporting for codepaths that do not explicitly return in a function. */
// "noFallthroughCasesInSwitch": true, /* Enable error reporting for fallthrough cases in switch statements. */
// "noUncheckedIndexedAccess": true, /* Add 'undefined' to a type when accessed using an index. */
"noImplicitOverride": true /* Ensure overriding members in derived classes are marked with an override modifier. */,
"noImplicitReturns": true,
"noPropertyAccessFromIndexSignature": true,
"noUncheckedIndexedAccess": true /* Add 'undefined' to a type when accessed using an index. */,
"noUnusedLocals": true,
"noUnusedParameters": true,
/* Modules */
Expand Down

0 comments on commit 6fcbadf

Please sign in to comment.