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

Add support of isDefaultValueAvailable and getDefaultValue on PropertyInfo and Parameter types #66

Open
leohubert opened this issue Aug 28, 2022 · 6 comments
Labels
enhancement New feature or request in-progress
Milestone

Comments

@leohubert
Copy link

Hi all!

In a DI project, I need to know if a parameter in a class constructor function has a default value.
And also I need to be able to get the default value if needed.

This can be useful for classic methods or class properties.

@Hookyns Hookyns added the enhancement New feature or request label Aug 28, 2022
@Hookyns
Copy link
Owner

Hookyns commented Aug 29, 2022

Detect if the parameter has initializer (that's how TypeScript compiler calls default value) is simple, if the parameter is rest parameter (...foo) too. But I have to think about the default value, because it is not easy to get it in "static" context,.. somewhere in the metadata.

Imagine that the initializer of the parameter is class instantiation. If you reflect over such function/method from other file, you will not have reference to that class. So you have to import it dynamically, that means import(): Promise.

What if that class is not exported from that module?

I'm able to get static values such as string, number and boolean literals as I do it in decorators.

@Hookyns
Copy link
Owner

Hookyns commented Aug 29, 2022

Hmm this will be the most problematic case IMHO.

class Foo {
	constructor(private _foo: any) {}

	foo(foo: any = this._foo) {
		
	}
}

Initializer is this._foo. That cannot be solved.

@Hookyns
Copy link
Owner

Hookyns commented Sep 29, 2022

I implemented the simple part in v1 (detection if the parameter is the rest parameter, if it has initializer and serialization of constant initializer).

@Hookyns
Copy link
Owner

Hookyns commented Sep 29, 2022

Your investigation around param = new Something() initializers is still relevant.

@leohubert
Copy link
Author

Your investigation around param = new Something() initializers is still relevant.

I think I have a lead.

@Hookyns
Copy link
Owner

Hookyns commented Oct 3, 2022

@leohubert
What if we do something like this in runtime?

class ParameterInfo {
    ....
    +++ get initializer(): Initializer;
    ...
}

class Initializer {
    isConstant(): this is ConstantInitializer;
    isInstance(): this is InstanceInitializer ;
}

class ConstantInitializer extends Initializer {
    get value(): any;
}

class InstanceInitializer extends Initializer {
    get type(): Type;
    getValue(): Promise<any>;
}

Transformer just has to detect the type; maybe the constant arguments too?
In the v1 this is super easy solution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request in-progress
Projects
None yet
Development

No branches or pull requests

2 participants