Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support higher order inferences for constructor functions #31116

Merged
merged 8 commits into from Apr 30, 2019

Conversation

Projects
None yet
4 participants
@ahejlsberg
Copy link
Member

commented Apr 25, 2019

This PR expands on #30215 to support higher order inferences for constructor functions and to permit function type arguments to higher order composition functions to have other members in addition to their single call signature.

Some examples:

declare class Point {
    constructor(x: number, y: number);
    readonly x: number;
    readonly y: number;
}

declare class Bag<T> {
    constructor(...args: T[]);
    contains(value: T): boolean;
    static foo: string;
}

function asFunction<A extends any[], B>(cf: new (...args: A) => B) {
    return (...args: A) => new cf(...args);
}

const newPoint = asFunction(Point);  // (x: number, y: number) => Point
const newBag = asFunction(Bag);      // <T>(...args: T[]) => Bag<T>
const p1 = new Point(10, 20);        // Point
const p2 = newPoint(10, 20);         // Point
const bag1 = new Bag(1, 2, 3);       // Bag<number>
const bag2 = newBag('a', 'b', 'c');  // Bag<string>

declare class Component<P> {
    props: P;
    constructor(props: P);
}

type ComponentClass<P> = new (props: P) => Component<P>;

declare function myHoc<P>(C: ComponentClass<P>): ComponentClass<P>;

type GenericProps<T> = { foo: number, stuff: T };

declare class GenericComponent<T> extends Component<GenericProps<T>> {}

// const GenericComponent2: new <T>(props: GenericProps<T>) => Component<GenericProps<T>>
const GenericComponent2 = myHoc(GenericComponent);

The rules for determining when higher order inferences occur are revised as follows. Given a generic function f of type <…>(…, x: P, …) => R, and an argument expression a of a generic function type A, in a call expression f(…, a, …) the type parameters of A are propagated onto the inferred result type of the function call if:

  • P is a function type with no type arguments, a single call or construct signature, and no other members, and
  • R is a function type with no type arguments, a single call or construct signature, and no other members, and
  • A is function type with type arguments and a single call or construct signature that matches P (note that A is permitted to have other members), and
  • in the left-to-right processing of the function call arguments, no inferences have been made for any of the type parameters referenced in P.

Fixes #30650.

@ahejlsberg

This comment has been minimized.

Copy link
Member Author

commented Apr 25, 2019

@typescript-bot test this

@typescript-bot

This comment has been minimized.

Copy link
Collaborator

commented Apr 25, 2019

Heya @ahejlsberg, I've started to run the extended test suite on this PR at fcd6f52. You can monitor the build here. It should now contribute to this PR's status checks.

@ahejlsberg

This comment has been minimized.

Copy link
Member Author

commented Apr 25, 2019

@typescript-bot

This comment has been minimized.

Copy link
Collaborator

commented Apr 25, 2019

Heya @ahejlsberg, I've started to run the parallelized Definitely Typed test suite on this PR at fcd6f52. You can monitor the build here. It should now contribute to this PR's status checks.

@ahejlsberg

This comment has been minimized.

Copy link
Member Author

commented Apr 25, 2019

@typescript-bot

This comment has been minimized.

Copy link
Collaborator

commented Apr 25, 2019

Heya @ahejlsberg, I've started to run the parallelized Definitely Typed test suite on this PR at 4fe59dc. You can monitor the build here. It should now contribute to this PR's status checks.

@weswigham

This comment has been minimized.

Copy link
Member

commented Apr 25, 2019

The react-test-renderer failure is in master, so I think DT is clean.

@ahejlsberg

This comment has been minimized.

Copy link
Member Author

commented Apr 26, 2019

@typescript-bot

This comment has been minimized.

Copy link
Collaborator

commented Apr 26, 2019

Heya @ahejlsberg, I've started to run the parallelized Definitely Typed test suite on this PR at 3e79e8d. You can monitor the build here. It should now contribute to this PR's status checks.

ahejlsberg added some commits Apr 27, 2019

@ahejlsberg ahejlsberg merged commit 9005449 into master Apr 30, 2019

5 checks passed

continuous-integration/travis-ci/pr The Travis CI build passed
Details
license/cla All CLA requirements met.
Details
node10 Build #29219 succeeded
Details
node11 Build #29217 succeeded
Details
node8 Build #29218 succeeded
Details

@ahejlsberg ahejlsberg deleted the higherOrderConstructorTypes branch Apr 30, 2019

@DanielRosenwasser DanielRosenwasser removed this from This Week in Design Meeting Docket May 24, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.