Skip to content

style "this" parameter differently from regular parameters #959

@chharvey

Description

@chharvey

Background

Since TypeScript 2.0, you can provide a this parameter, a “fake parameter” to functions, to specify what the type of this is when referenced inside the function.

function getThisURL(this: Window): string {
	return this.location.href // no error
}
getThisURL(); // returns 'https://github.com/...'

The function above doesn’t throw a compile-time error because this.location is guaranteed to exist (and be a Location object), since we specified the type of this as Window.

Obviously, getThisURL does not have any regular parameters—it doesn’t take any arguments.

Problem

Example:

/**
 * The identity function.
 * @this    any
 * @param   <T> the type of the argument
 * @param   foo an argument
 * @returns the argument
 */
export function identity<T>(this: any, foo: T): T {
	return foo
}

TypeDoc output of the above function: the `this` parameter is styled the same as a real parameter.

The function above has a “fake” this parameter and a “real” foo parameter—it’s only expecting one argument when called, e.g. identity(42). In the TypeDoc rendering, both parameters are styled the same, which is confusing because it leads readers to believe that two arguments should be passed to the function when calling it.

Suggested Solution

I’m not a designer, so I probably don’t have any good design suggestions, but all I ask is that the this parameter is somehow highlighted or separated from the real parameters of the function. As shown above, Type Parameters have their own section, so maybe the this parameter should too.

Metadata

Metadata

Assignees

No one assigned

    Labels

    help wantedContributions are especially encouraged

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions