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

Methods of classes extended in others do not inherit the types of method arguments. #58611

Closed
1 task done
DMVMarcio opened this issue May 22, 2024 · 4 comments
Closed
1 task done
Labels
Duplicate An existing issue was already created

Comments

@DMVMarcio
Copy link

DMVMarcio commented May 22, 2024

Acknowledgement

  • I acknowledge that issues using this template may be closed without further explanation at the maintainer's discretion.

Comment

TypeScript Version: 5.3.3

When extending a class and overriding a method in the new class, it does not inherit the types of the original method, however, it forces you to maintain the same arguments, the same types, async and the same return, making it necessary to re-write the typing.

If something is required to be exactly the same thing, both arguments and return, shouldn't it automatically inherit the types?

Example:

interface Data {
	id: string;
	name: string;
}```

```ts
class Test1 {
	exec(data: Data) {
		console.log(data.id);
	}
}

class Test2 extends Test1 {
    exec(data) { // data: any
        console.log(data.name);
    }
}

class Test2 extends Test1 {
	constructor() {
        super();
    }

    exec = (data) => {} // data: any
}

class Test2 extends Test1 {
	exec(data: number) {} // Property 'exec' in type 'Test2' is not assignable to the same property in base type 'Test1'.
}

Works:

class Test2 extends Test1 {
	constructor() {
        super();

        this.exec = (data) => {} // data: Data
    }
}```
@fatcerberus
Copy link

Duplicate of #32082 and/or other issues linked therein.
See also: #36165.

@DMVMarcio
Copy link
Author

This "solved" the problem, however, this still leaves the need to add some unnecessary typing.

@fatcerberus
Copy link

This "solved" the problem

I'm not sure what you're referring to; I was just pointing out other issues requesting the same or similar features. This indeed isn't possible today.

@andrewbranch andrewbranch added the Duplicate An existing issue was already created label May 22, 2024
@typescript-bot
Copy link
Collaborator

This issue has been marked as "Duplicate" 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 May 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Duplicate An existing issue was already created
Projects
None yet
Development

No branches or pull requests

4 participants