🤖 Merge PR #47117 [meteor-mdg-validated-method] Add ThisType annotation by @artemiswkearney

`run` and `validate` now have strongly typed `this`
This commit is contained in:
Artemis Kearney 2020-09-18 14:28:12 -07:00 committed by GitHub
parent 65d65963e5
commit c46be2bbd9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 59 additions and 5 deletions

View File

@ -17,6 +17,40 @@ declare module 'meteor/mdg:validated-method' {
export type ValidatedMethodArg<T> = T extends ValidatedMethod<string, infer TRun> ? Argument<TRun> : never;
export type ValidatedMethodReturn<T> = T extends ValidatedMethod<string, infer TRun> ? Return<TRun> : never;
export interface ValidatedMethodThisBase {
/**
* @summary Access inside a method invocation. Boolean value, true if this invocation is a stub.
* @locus Anywhere
*/
isSimulation: boolean;
/**
* @summary Call inside a method invocation. Allow subsequent method from this client to begin running in a new fiber.
* @locus Server
*/
unblock(): void;
/**
* @summary The id of the user that made this method call, or `null` if no user was logged in.
* @locus Anywhere
*/
userId: string | null;
/**
* @summary Set the logged in user.
* @locus Server
* @param userId The value that should be returned by `userId` on this connection.
*/
setUserId(userId: string | null): void;
/**
* @summary Access inside a method invocation. The [connection](#meteor_onconnection) that this method was received on. `null` if the method is not associated with a connection, eg. a server
* initiated method call. Calls to methods made from a server method which was in turn initiated from the client share the same `connection`.
* @locus Server
*/
connection: Meteor.Connection;
/**
* @summary The seed for randomStream value generation
*/
randomSeed(): string;
}
/**
* When declaring a mixin that adds fields to ValidatedMethodOptions, augment this to add them
*/
@ -71,7 +105,7 @@ declare module 'meteor/mdg:validated-method' {
> = TOptions extends ValidatedMethodOptions<any, infer TRun> ? Argument<TRun> : never;
export class ValidatedMethod<TName extends string, TRun extends (...args: any[]) => any> {
constructor(options: ValidatedMethodOptionsWithMixins<TName, TRun>);
constructor(options: ValidatedMethodOptionsWithMixins<TName, TRun> & ThisType<ValidatedMethodThisBase & { name: TName extends string ? TName : string }>);
call: Argument<TRun> extends NoArguments
? // methods with no argument can be called with () or just a callback
((unusedArg: any, callback: (error: Meteor.Error, result: Return<TRun>) => void) => void) &

View File

@ -201,7 +201,27 @@ new ValidatedMethod({
});
// method can access its name
// TODO "slice" call is needed here because this method actually returns `Return<() => TName> & string` and I don't know why
// (the `& string` was something I added so you can at least use the name in weird situations like this, but I don't get why TName isn't getting resolved there - it's clearly known by now!)
// $ExpectType string
methodReturnsName.call().slice();
// $ExpectType "methodReturnsName"
methodReturnsName.call();
// method has all expected "this" properties
new ValidatedMethod({
name: 'methodThatUsesThis',
validate: null,
run() {
// $ExpectType "methodThatUsesThis"
this.name;
// $ExpectType boolean
this.isSimulation;
// $ExpectType string | null
this.userId;
// $ExpectType void
this.setUserId("test");
// $ExpectType Connection
this.connection;
// $ExpectType string
this.randomSeed();
// $ExpectType void
this.unblock();
}
});