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

use typeof on return type with T #22030

Closed
bluelovers opened this issue Feb 19, 2018 · 4 comments
Closed

use typeof on return type with T #22030

bluelovers opened this issue Feb 19, 2018 · 4 comments
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug

Comments

@bluelovers
Copy link
Contributor

TypeScript Version: 2.7.0-dev.201xxxxx

Search Terms:

Code

can we use typeof on return type with T

// @ts-ignore
export function getStatic<T>(target: T): typeof T
{
	// @ts-ignore
	return target.__proto__.constructor;
}

but at real case i have write

// @ts-ignore
export function getStatic<T>(target): T
{
	// @ts-ignore
	return target.__proto__.constructor;
}

export class A
{
	static b()
	{
		console.log('this is static b');
	}

	b()
	{
		console.log('this is b');
	}
}

export let a = new A();
export const c = getStatic<typeof A>(a);

c.b();
a.b();

Expected behavior:

export declare function getStatic<T>(target: T): typeof T;

Actual behavior:

export declare function getStatic<T>(target: T): T;

Playground Link:

Related Issues:

@ghost
Copy link

ghost commented Feb 20, 2018

typeof x expects to get the type of a variable x. You can't get the typeof a type.
You can accomplish what you want like so:

class C {
	readonly "constructor"!: typeof C;
	static m() {}
}

new C().constructor.m();

@ghost ghost added the Working as Intended The behavior described is the intended behavior; this is not a bug label Feb 20, 2018
@bluelovers
Copy link
Contributor Author

@andy-ms what is !: mean?

@ghost
Copy link

ghost commented Feb 21, 2018

It's a definite assignment assertion (#20166)

@typescript-bot
Copy link
Collaborator

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

@microsoft microsoft locked and limited conversation to collaborators Jul 25, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Working as Intended The behavior described is the intended behavior; this is not a bug
Projects
None yet
Development

No branches or pull requests

2 participants