Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Method signatures containing references to properties of constrained generic types are resolving through the constraint instead of the supplied type argument #12651

Copy link
Copy link
@rotemdan

Description

@rotemdan
Issue body actions

TypeScript Version: nightly (2.2.0-dev.20161203)

I currently find it somewhat hard to precisely describe the issue/suggestion, but I believe this use case should demonstrate it clearly:

type MethodDescriptor = {
	name: string;
	args: any[];
	returnValue: any;
}

function dispatchMethod<M extends MethodDescriptor>(name: M['name'], args: M['args']): M['returnValue'];

type SomeMethodDescriptor = {
	name: "someMethod";
	args: [number, string];
	returnValue: string[];
}

// Currently, there's no compilation error on the the invalid second argument type and the 
// return type is inferred as "any" instead of "string[]":
let result = dispatchMethod<SomeMethodDescriptor>("someMethod", ["hello", 35]);

The general idea here is that the types are used unconventionally: not to describe interfaces to run-time objects but as a way to "pack" a set of type arguments into an "object-like" compile-time entity. Perhaps this could be called something like "packed type arguments" or "type argument classes" or maybe "type argument schema containers"? I'm not sure.

Anyway, I was trying to write a general purpose dispatcher that can invoke a set of actions, and that seemed like a more elegant and type safe alternative to the more conventional:

function dispatchMethod<A extends any[], R>(name: string, args: A): R;

dispatchMethod<[number, string], string[]>("someMethod", ["hello", 35]); // <- errors as expected
Reactions are currently unavailable

Metadata

Metadata

Assignees

No one assigned

    Labels

    FixedA PR has been merged for this issueA PR has been merged for this issue

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      Morty Proxy This is a proxified and sanitized view of the page, visit original site.