Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions sdks/ts/packages/golem-ts-sdk/src/baseAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,11 +109,11 @@ export class BaseAgent {
*/
async loadSnapshot(bytes: Uint8Array): Promise<void> {
const text = new TextDecoder().decode(bytes);
const state = JSON.parse(text);
const state = JSON.parse(text) as Partial<this>;

for (const [k, v] of Object.entries(state)) {
if (k === 'cachedAgentType' || k === 'agentClassName') continue;
(this as any)[k] = v;
this[k as keyof this] = v;
}
}

Expand Down Expand Up @@ -230,15 +230,17 @@ export class BaseAgent {
*
* ```
*/
type MethodKeys<T> = {
[K in keyof T]-?: T[K] extends (...args: never[]) => unknown ? K : never;
}[keyof T];

export type Client<T> = {
[K in keyof T as T[K] extends (...args: any[]) => any ? K : never]: T[K] extends (
...args: infer A
) => infer R
[K in MethodKeys<T>]: T[K] extends (...args: infer A) => infer R
? RemoteMethod<GetArgs<A>, Awaited<R>>
: never;
};

export type RemoteMethod<Args extends any[], R> = {
export type RemoteMethod<Args extends unknown[], R> = {
(...args: Args): Promise<R>;
trigger: (...args: Args) => void;
schedule: (ts: Datetime, ...args: Args) => void;
Expand All @@ -264,8 +266,8 @@ type GetArgs<T extends readonly unknown[]> =
type IsOptional<T extends readonly unknown[], K extends keyof T> =
{} extends Pick<T, K> ? true : false;

type AllOptional<T extends readonly unknown[], I extends any[] = []> = T extends readonly [
any,
type AllOptional<T extends readonly unknown[], I extends unknown[] = []> = T extends readonly [
unknown,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again no much safety introduction.
But this version is not harming. Hence approving

...infer R,
]
? IsOptional<T, I['length']> extends true
Expand Down
10 changes: 5 additions & 5 deletions sdks/ts/packages/golem-ts-sdk/src/host/result.ts
Original file line number Diff line number Diff line change
Expand Up @@ -320,25 +320,25 @@ function flatten<T, E, E2>(this: Result<Result<T, E>, E2>): Result<T, E | E2> {
else return this.val;
}

function assertErrorInstanceOf<T, C extends abstract new (..._: any) => any>(
function assertErrorInstanceOf<T, C extends abstract new (..._: unknown[]) => unknown>(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Again not much value add. It doesn't provide extra safety, but not "against" this change just because it kind of look better than previous version

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any is not unknown. Sometimes any does makes more sense.

this: Result.Ok<T>,
constructor: C,
): Result.Ok<T>;
function assertErrorInstanceOf<E, C extends abstract new (..._: any) => any>(
function assertErrorInstanceOf<E, C extends abstract new (..._: unknown[]) => unknown>(
this: Result.Err<E>,
constructor: C,
): Result.Err<E & InstanceType<C>>;
function assertErrorInstanceOf<T, E, C extends abstract new (..._: any) => any>(
function assertErrorInstanceOf<T, E, C extends abstract new (..._: unknown[]) => unknown>(
this: Result<T, E>,
constructor: C,
): Result<T, E & InstanceType<C>>;
function assertErrorInstanceOf<T, E, C extends abstract new (..._: any) => any>(
function assertErrorInstanceOf<T, E, C extends abstract new (..._: unknown[]) => unknown>(
this: Result<T, E>,
constructor: C,
): Result<T, E & InstanceType<C>> {
if (this.isOk()) return this;

if (this.val instanceof constructor) return this as any;
if (this.val instanceof constructor) return this as Result<T, E & InstanceType<C>>;

throw new TypeError(`Assertion failed: Expected error to be an instance of ${constructor.name}.`);
}
4 changes: 2 additions & 2 deletions sdks/ts/packages/golem-ts-sdk/src/host/transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,6 @@ export function fallibleTransaction<Out, Err>(
* ```
*
*/
export type OperationErrors<T extends Operation<any, any, any>[]> = {
[K in keyof T]: T[K] extends Operation<any, any, infer Err> ? Err : never;
export type OperationErrors<T extends Operation<unknown, unknown, unknown>[]> = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't give any extra safety, but I am not totally against this change

[K in keyof T]: T[K] extends Operation<unknown, unknown, infer Err> ? Err : never;
}[number];
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ const handlers: { [K in TsType['kind']]: Handler<K> } = {
config: unsupported('Config'),
};

function unsupported(kind: string): Handler<any> {
function unsupported<K extends TsType['kind']>(kind: string): Handler<K> {
return ({ scopeName, parameterInScope }) =>
Either.left(
`Unsupported type \`${kind}\`` +
Expand All @@ -79,7 +79,7 @@ function unsupported(kind: string): Handler<any> {
);
}

function unsupportedWithHint(kind: string, hint: string): Handler<any> {
function unsupportedWithHint<K extends TsType['kind']>(kind: string, hint: string): Handler<K> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

return ({ scopeName, parameterInScope }) =>
Either.left(
`Unsupported type \`${kind}\`${scopeName ? ` in ${scopeName}` : ''}` +
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export class WitTypeBuilder {
}

private convert(typ: AnalysedType): WitTypeNode {
let kind = typ.kind;
switch (typ.kind) {
case 'variant': {
const cases: [string, NodeIndex | undefined][] = typ.value.cases.map(
Expand Down Expand Up @@ -127,7 +128,7 @@ export class WitTypeBuilder {
}

default:
throw new Error(`Unhandled AnalysedType kind: ${(typ as any).kind}`);
throw new Error(`Unhandled AnalysedType kind: ${kind}`);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import * as util from 'node:util';
import { NameOptionTypePair } from '../types/analysedType';

// type mismatch in tsValue when converting from TS to WIT
export function typeMismatchInSerialize(tsValue: any, expectedType: string): string {
export function typeMismatchInSerialize(tsValue: unknown, expectedType: string): string {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you think about this, it's not unknown. its any

return `Type mismatch. Expected type \`${safeDisplay(expectedType)}\`, got \`${safeDisplay(tsValue)}\``;
}

Expand All @@ -30,23 +30,23 @@ export function typeMismatchInDeserialize(nodeTag: string, expectedType: string)
}

// Missing keys in tsValue when converting from TS to WIT
export function missingObjectKey(key: string, tsValue: any): string {
export function missingObjectKey(key: string, tsValue: unknown): string {
return `Missing key '${key}' in ${safeDisplay(tsValue)}`;
}

// tsValue does not match any of the union types when converting from TS to WIT
export function unionTypeMatchError(unionTypes: NameOptionTypePair[], tsValue: any): string {
export function unionTypeMatchError(unionTypes: NameOptionTypePair[], tsValue: unknown): string {
const types = unionTypes.map((t) => t.name);
return `Value '${safeDisplay(tsValue)}' does not match any of the union types: ${types.join(', ')}`;
}

export function enumMismatchInSerialize(enumValues: string[], tsValue: any): string {
export function enumMismatchInSerialize(enumValues: string[], tsValue: unknown): string {
return `Value '${safeDisplay(tsValue)}' does not match any of the enum values: ${enumValues.join(', ')}`;
}

// unhandled type of tsValue when converting from TS to WIT
export function unhandledTypeError(
tsValue: any,
tsValue: unknown,
typeName: string | undefined,
message: string | undefined,
): string {
Expand All @@ -57,6 +57,6 @@ export function unhandledTypeError(
return error + (message ? `${message}` : '');
}

export function safeDisplay(tsValue: any): string {
export function safeDisplay(tsValue: unknown): string {
return util.format(tsValue);
}
2 changes: 1 addition & 1 deletion sdks/ts/packages/golem-ts-sdk/src/webhook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export class WebhookHandler implements PromiseLike<WebhookRequestPayload> {

then<TResult1 = WebhookRequestPayload, TResult2 = never>(
onfulfilled?: ((value: WebhookRequestPayload) => TResult1 | PromiseLike<TResult1>) | null,
onrejected?: ((reason: any) => TResult2 | PromiseLike<TResult2>) | null,
onrejected?: ((_reason: unknown) => TResult2 | PromiseLike<TResult2>) | null,
): Promise<TResult1 | TResult2> {
return this.wait().then(onfulfilled, onrejected);
}
Expand Down
Loading