Skip to content

Error "The 'this' context of type 'void' is not assignable to method's 'this' ..." when using an intermediate function instead of "!" #23304

Description

@stephanedr

Same situation than #23040, but using an intermediate function instead of using "!".

TypeScript Version: 2.9.0-dev.20180410

Search Terms: "'this' context of type 'void' is not assignable"

Code

interface Foo {
    required: ((this: Foo) => void);
    optional: ((this: Foo) => void) | null;
}

function notNull<T>(value: T): NonNullable<T> {
    if (value == null) throw Error("notNull() failed");
    return value as NonNullable<T>;
}

declare const foo: Foo;
foo.required();
foo.optional!();   // Fixed by #23040.
notNull(foo.optional)();  // Error.

Expected behavior:
No Error.

Actual behavior:
The 'this' context of type 'void' is not assignable to method's 'this' of type 'Foo'.

Playground Link: https://www.typescriptlang.org/play/index.html#src=interface%20Foo%20%7B%0D%0A%20%20%20%20required%3A%20((this%3A%20Foo)%20%3D%3E%20void)%3B%0D%0A%20%20%20%20optional%3A%20((this%3A%20Foo)%20%3D%3E%20void)%20%7C%20null%3B%0D%0A%7D%0D%0A%0D%0Afunction%20notNull%3CT%3E(value%3A%20T)%3A%20NonNullable%3CT%3E%20%7B%0D%0A%20%20%20%20if%20(value%20%3D%3D%20null)%20throw%20Error(%22notNull()%20failed%22)%3B%0D%0A%20%20%20%20return%20value%20as%20NonNullable%3CT%3E%3B%0D%0A%7D%0D%0A%0D%0Adeclare%20const%20foo%3A%20Foo%3B%0D%0Afoo.required()%3B%0D%0Afoo.optional!()%3B%20%20%20%2F%2F%20Fixed%20by%20%2323040.%0D%0AnotNull(foo.optional)()%3B%20%20%2F%2F%20Error.%0D%0A

Related Issues: #23040

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions