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

Allow class decorators to add static properties. #40805

Closed
Griffork opened this issue Sep 28, 2020 · 3 comments
Closed

Allow class decorators to add static properties. #40805

Griffork opened this issue Sep 28, 2020 · 3 comments
Labels
Duplicate An existing issue was already created

Comments

@Griffork
Copy link

Search Terms

Class, decorators, typing.

Suggestion

I have a decoator which is typed like so to add a static method to a decorated class. I would like the decorator's typing information to be evaluated and added to the class' typing information.

Decorator typing information (Biscuit is the name of my engine, don't judge I'm not good at naming things):

type ConstructArgs<T> = T extends (new (...args: infer C) => any)?C: never;
type Prototype<T> = (Function & { prototype: T });

//First set of params are the ones supplied in the decorator, like: @BiscuitObject(common);
declare function BiscuitObject<SerialiseType extends Object>(datatype: SerialiseType): (
		//Second set of params are the class the decorator is decorating, in this case the BiscuitObject.
		<
			BiscuitClass extends Function & { prototype: Object },
			Biscuit extends Prototype<BiscuitClass>,
			ConstructorArguments extends ConstructArgs<Biscuit>,
		>
			(biscuit: BiscuitClass) => BiscuitClass & {
				//We return a deserialise function that takes some data and converts it
                // into a new object of this class type.
				deserialise: (object: SerialiseType, ...args: ConstructorArguments) =>	Biscuit;
	});

Example code:

class BDataType {
    tell!: string;
    constructor(tell: string) {
        this.tell = tell;
    }
}

@BiscuitObject(BDataType)
class B {
    tell: string;
    param: string;
    constructor(param: string) {
        this.param = param;
        this.tell = "hi";
    }
    serialise() {
        return new BDataType(this.tell);
    }
	method() {
		console.log(this.tell, this.param);
	}
}

B.deserialise(new BDataType("world"), "hello").method(); //This line errors currently.

https://www.typescriptlang.org/play?#

Use Cases

I want to use this to automate the addition of several common static functions that I use across about 20 different classes in my codebase.

Checklist

My suggestion meets these guidelines:

  • [y] This wouldn't be a breaking change in existing TypeScript/JavaScript code
  • [y] This wouldn't change the runtime behavior of existing JavaScript code
  • [y] This could be implemented without emitting different JS based on the types of the expressions
  • [y] This isn't a runtime feature (e.g. library functionality, non-ECMAScript syntax with JavaScript output, etc.)
  • [y] This feature would agree with the rest of TypeScript's Design Goals.
@MartinJohns
Copy link
Contributor

MartinJohns commented Sep 28, 2020

Duplicate of #4881.

The TypeScript team also has said on several occasions that they will not expand on the experimental decorator support until TC39 standardized the decorator support.

We're not expanding the current experimental decorator implementation any further since this is something we're trying to standardize through the TC39 proposal. Suggestions for more behavior there should be taken to the tc39 proposal repo

Source

@RyanCavanaugh RyanCavanaugh added the Duplicate An existing issue was already created label Sep 28, 2020
@Griffork
Copy link
Author

Sorry! I did a search and found two related (but closed issues) so I made this. Thanks for that reference about TC39, that's really helpful to know.

@typescript-bot
Copy link
Collaborator

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

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