镜像站点 · 本页由第三方 GitHub 只读镜像提供,非 GitHub 官方站点,不接受任何登录或凭据输入。前往 github.com
Skip to content

strictFunctionTypes option should strictly check callback parameters of override methods #19007

Description

@falsandtru

As Aluan Haddad (@aluanhaddad) mentioned at #19004 (comment).

cc Anders Hejlsberg (@ahejlsberg)

TypeScript Version: master

Code

class C {
  a(f: (a: C) => C): void {
  }
}
class D extends C {
  p: boolean = true;
  a(f: (a: D) => D): void { // should throw an error here.
    console.log(this.p);
    this.p = f(new D()).p; // type is boolean, but value is undefined.
    console.log(this.p);
  }
}

const c: C = new D();
c.a(() => new C());

Expected behavior:

error

Actual behavior:

$ node built/local/tsc.js index.ts --strictFunctionTypes && node index.js
true
undefined

Activity

  1. ahejlsberg commented on Oct 7, 2017

    @ahejlsberg
    Member

    This is working as intended. As explained here, in --strictFunctionTypes mode we still check parameters of methods and constructors bivariantly. So, there is no error here:

    interface C {
      a(f: (a: C) => C): void;
    }
    interface D extends C {
      p: boolean;
      a(f: (a: D) => D): void;
    }

    But an error is reported here:

    interface C {
      a: (f: (a: C) => C) => void;
    }
    interface D extends C {
      p: boolean;
      a: (f: (a: D) => D) => void;
    }

    As I explain here, we can't tighten this any further without causing Array<T> and other core types to become invariant.

  2. falsandtru commented on Oct 7, 2017

    @falsandtru
    ContributorAuthor

    I see, thanks for your explanation.

  3. locked and limited conversation to collaborators on Jun 14, 2018
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