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

Cannot access properties of type parameter constrainted by 'any' #7700

Closed
andrewvarga opened this issue Mar 26, 2016 · 4 comments
Closed

Cannot access properties of type parameter constrainted by 'any' #7700

andrewvarga opened this issue Mar 26, 2016 · 4 comments
Assignees
Labels
Bug A bug in TypeScript

Comments

@andrewvarga
Copy link

I have a problem where if I use a generic function which extends from another generic type, the function parameter's type doesn't seem to be respected.

TypeScript Version:

1.8.9 (current stable)

Code

// Generic Tree structure (couldn't achieve the same with interfaces / classes)
type Tree<T> = T & {
    children?: Tree<T>[];
}

interface INode 
{
    id: number;
}

var myTree: Tree<INode> = {
    id: 1,
    children: [
        {
            id: 2
        }
    ]
};

// Compiles and works nicely:
console.log(myTree.id);
console.log(myTree.children[0].id);

class MyClass
{
    public static displayTree1<T extends Tree<any>>(tree: T)
    {
        // Compile time error "Property 'children does' not exist on type 'T'"
        console.log(tree.children);
    }

    public static displayTree2<T extends Tree<INode>>(tree: T)
    {
        // Compiles fine
        console.log(tree.children);
    }

    public static displayTree3(tree: Tree<any>)
    {
        // Compiles fine
        console.log(tree.children);
    }
}

Expected behavior:

I expect the "displayTree1" function to compile fine, T extends Tree so it can be assumed to have a 'children' property.

Actual behavior:

Compile time error: Property 'children does' not exist on type 'T'.

@DanielRosenwasser
Copy link
Member

The problems seems to be when any shows up in the constraint of a type parameter.

Simpler repro:

function f<T extends any>(x: T) {
    // Error "Property 'children does' not exist on type 'T'"
    console.log(x.children);

    // Error: x has no call signatures 
    x();

    // But the compiler says that these are okay?
    new x();
    x[100];
    x["hello"];
}

@DanielRosenwasser
Copy link
Member

I'm going to say this is a bug, but @RyanCavanaugh and others can weigh in.

@DanielRosenwasser DanielRosenwasser added the Bug A bug in TypeScript label Mar 26, 2016
@DanielRosenwasser DanielRosenwasser changed the title Generic static function's parameter type extending another generic type - compile error Cannot access properties of type parameter constrainted by 'any' Mar 26, 2016
@RyanCavanaugh RyanCavanaugh added Suggestion An idea for TypeScript In Discussion Not yet reached consensus labels Mar 28, 2016
@RyanCavanaugh
Copy link
Member

It's behaving according to spec. x: T acquires the apparent members of any; the behavior of all property accesses / functions calls being allowed on any is a result of the language in the checking of those respective definitions.

We should probably just change that language to say "is any or a type parameter constrained to any"

@mhegazy mhegazy removed the Bug A bug in TypeScript label Apr 26, 2016
@mhegazy mhegazy added Bug A bug in TypeScript and removed In Discussion Not yet reached consensus Suggestion An idea for TypeScript labels May 17, 2016
@mhegazy mhegazy added this to the TypeScript 2.0 milestone May 17, 2016
@sandersn
Copy link
Member

The fix is up at #8770

@microsoft microsoft locked and limited conversation to collaborators Jun 19, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Bug A bug in TypeScript
Projects
None yet
Development

No branches or pull requests

5 participants