Skip to content

Classes static generic function return any type. #58293

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

Closed
lengyuxuan opened this issue Apr 23, 2024 · 2 comments
Closed

Classes static generic function return any type. #58293

lengyuxuan opened this issue Apr 23, 2024 · 2 comments
Labels
Question An issue which isn't directly actionable in code

Comments

@lengyuxuan
Copy link

πŸ”Ž Search Terms

  • Generics
  • Scope

πŸ•— Version & Regression Information

  • This is a crash
  • This changed between versions 3.3.3 and nightly

⏯ Playground Link

https://www.typescriptlang.org/play?#code/MYGwhgzhAECyD2ATApiaBvAUNH0AOArgEYgCWw0EALmFedAGakB2iAQgJ4AKA1gDyxoyAB5VkrGAhQgAfAApSiAFyUqAJxYBzAJQrBAH2jMCINFlwXoa5FQJrmGbJecB6F9AB0Xj0+cBfaEg4AG5faD9MCMxQSBgAQSFRcURJJFRHC0UVag1mTVCLZhEqAEllVVzNaENjUwLcYHhmHIJgKng1OW0M50oCPGRO7XrnKgALUggPRWgAXmgAcmFlhZHLccmPItEyucWOA9WwiKjG5qpAvaKAd2g4rtCQG2hgO2tmUvKcrWqjEzR5mBpohQtcJk9oHJXmp3p9uuZcG5oAB1MYcaCTRZgZgcBYAfjCZ2oRjSeziHiYrE4vD4cXk0NhZWGYVIDEhAEJmGl4WELERrGAeGsIhYGeJPlc0ltimU1kjUeJoCVoNcNGJFmKPrt5lyUNKdohAjBvnkFtBxrQFjANJoxhdIpggA

πŸ’» Code

class Model {
    public static findByPk<M extends Model>(id: string): M | null {
        return {
            // ....
        } as M;
    }
}

class A extends Model {
    id: string;
    nextId: string | null;
    constructor() {
        super();
        this.id = 'xxx';
        this.nextId = 'yyy';
    }
}

const a = new A();
let currentId: string | null = a.id;
while (currentId) {
    // Why is 'any'?
    const node = A.findByPk<A>(currentId);
    if (!node) {
        break;
    }
    currentId = node.nextId;
    // When I write 'currentId = node.nextId as string' that's right 
}

πŸ™ Actual behavior

Classes static generic function return any type.

πŸ™‚ Expected behavior

Classes static generic function return Model type.

Additional information about the issue

No response

@MartinJohns
Copy link
Contributor

This is a crash.
This changed between versions 3.3.3 and nightly

This is neither a crash (means the compiler process crashes and can't produce a result, an error being a valid result) and it didn't change between version 3.3.3 and nightly (a comically large version range).

// Why is 'any'?

The error message tells you why it's any:

'node' implicitly has type 'any' because it does not have a type annotation and is referenced directly or indirectly in its own initializer.

If you search for this error you will find many issues, e.g. this one: #43047 (comment)

@RyanCavanaugh RyanCavanaugh added the Question An issue which isn't directly actionable in code label Apr 23, 2024
@typescript-bot
Copy link
Collaborator

This issue has been marked as "Question" and has seen no recent activity. It has been automatically closed for house-keeping purposes.

@typescript-bot typescript-bot closed this as not planned Won't fix, can't repro, duplicate, stale Apr 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Question An issue which isn't directly actionable in code
Projects
None yet
Development

No branches or pull requests

4 participants