Skip to content

When using type to define shape of function, this context setting doesn't work #25975

@ffxsam

Description

@ffxsam

TypeScript Version: 2.9.1-insiders.20180525

Search Terms:
function type this

Code

interface PasswordReset {
    working: string;
}

type Validator = (
  this: PasswordReset,
  rule: any,
  value: any,
  callback: (error?: Error) => void
) => string;

const someFunc: Validator = function(rule, value, callback) {
    console.log(this.working === true);
    return 'OK';
};

interface OtherInterface {
    working: string;
}

function thisWorks(this: OtherInterface, name: string) {
    console.log(this.wr === true); //works as expected
}

Expected behavior:
There should be contextual awareness of what interface 'this' refers to.

Actual behavior:
'this' is shown as type 'any' in someFunc func

Playground Link:
Link

Related Issues:

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