FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Generic static method to specific after extending class · Issue #17575 · microsoft/TypeScript · GitHub

Repository navigation

Generic static method to specific after extending class #17575

Description

TypeScript Version: 2.3.4, I would however expect 2.4.0 version to be failing too, as nothing is in the changelog about this

Code

class Person {
	constructor(public name: string) { }
}

class Base<T> {
	reference?: T
	name: string

	constructor(name: string) {
		//	Assign properties from T to this
		this.name = name
	}

	static fromObject<T extends { name: string }>(object: T) {
		const base = new Base<T>(object.name)
		base.reference = object
		return base
	}
}

class Master<T> extends Base<T> {

	static fromBase<T>(base: Base<T>) {
		const master = new Master<T>(base.name)
		master.reference = base.reference
		return master
	}

	static fromObject<T extends { name: string }>(object: T) {
		return Master.fromBase(Base.fromObject(object))
	}
}

class PersonMaster extends Master<Person> {

	constructor(person: Person) {
		super(person.name)
		this.reference = person
	}

	static fromBase(base: Base<Person>) {
		return new PersonMaster(base)
	}
}

Expected behavior:
Expecting PersonMaster.fromBase to accept PersonMaster as return value.

Actual behavior:
Error: (it will actually compile just fine, it will only flag class as error, or if corrected, will make bad assumptions about type)
Class static side typeof PersonMaster incorrectly extends base class static side typeof Master. Types of property fromBase are incompatible. Type (base: Base<Person>) => PersonMaster is not assignable to type <T>(base: Base<T>) => Master<{}>. Types of parameters base and base are incompatible. Type Base<T> is not assignable to Base<Person>. Type T is not assignable to type Person.

Activity

  1. masaeedu commented on Aug 3, 2017

    Contributor

    This error is actually correct. A function signature <T>(base: Base<T>) => any is more restrictive than the function signature (base: Base<Person>) => any. You can give anyone asking for a (base: Base<Person>) => any a <T>(base: Base<T>) => any, but not vice versa. Since you are the implementer, you are promising to supply a function that can accept a "Base of anything", then breaking that promise in your implementation of PersonMaster.

  2. masaeedu commented on Aug 3, 2017

    Contributor

    I'm not 100% sure, but I think you might just have accidentally added an extra <T> in Master.fromBase's definition.

  3. Akxe commented on Aug 3, 2017

    Author

    Yes, but since I go from Master<T> to Master<Person>, I am sure, that it won't need make base from any, I only need to make base from Person. And this code is as correct as it can be, there is only one error and removing <T> in fromBase<T>(base... will make an other error: Static members cannot reference class type parameters.

  4. masaeedu commented on Aug 3, 2017

    Contributor

    If you know that you only use it with Master<Person>, you can make the signature of Master<T>.fromBase static fromBase(base: Base<Person>). Currently the signature demands that each deriving class be able to accept a Base of anything, which is not the contract PersonMaster satisfies.

  5. Akxe commented on Aug 3, 2017

    Author

    I want to give the PersonMaster additional functions, that are specific only to the Person type.

  6. mhegazy commented on Sep 6, 2017

    Contributor

    Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed.

  7. locked and limited conversation to collaborators on Jun 14, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Working as IntendedThe behavior described is the intended behavior; this is not a bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions


      Back | FazBrowse Home | New Git URL