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

Generate class types with method statics merged as config statics property #6

Open
tomFlidr opened this issue Aug 12, 2020 · 0 comments
Labels
enhancement New feature or request

Comments

@tomFlidr
Copy link
Member

tomFlidr commented Aug 12, 2020

The new form could provide better refactoring possibilities with class names in string quotes only in class definition place,
not in instancing places anymore, because custom classes extended from Ext classes will be class definitions, not interface definitions anymore. But Statics insterfaces must be changed into type definitions:

// test definitions:
declare namespace Ext.Test {
	class TestClass {
		static $className?: string;
		$className?: string;
		constructor (cfg: Ext.Test.TestClass.Cfg);
		statics?: Ext.Test.TestClass.Statics;
	}
	interface StaticsBase {
		BASE_PROP?: string;
		[propertyName: string]: any;
	}
}
declare namespace Ext.Test.Other {
	interface Statics extends Ext.Test.StaticsBase {
		OTHER_PROP?: string;
	}
}
declare namespace Ext.Test.Another {
	interface Statics extends Ext.Test.StaticsBase {
		ANOTHER_PROP?: string;
	}
}
declare namespace Ext.Test.TestClass {
	type Statics = (
		(() => Ext.Test.Other.Statics & Ext.Test.Another.Statics) & 
		(Ext.Test.Other.Statics & Ext.Test.Another.Statics)
	);
	interface Def {
		extends?: string;
		statics?: Ext.Test.TestClass.Statics;
	}
	interface Cfg {
		base?: string;
		other?: string;
		another?: string;
	}
}
// MyInstance.ts
declare namespace My.Custom.Namespace {
	class MyInstance extends Ext.Test.TestClass {
		static MyPublicStaticMethod (): void;
		other?: string;
		MyPublicInstanceMethod? (): void; // to declare '?' means it's not necessary to override in extended class
	}
}
Ext.define(
	'My.Custom.Namespace.MyInstance', 
	<My.Custom.Namespace.MyInstance & Ext.Test.TestClass.Def>{
		statics: <Ext.Test.TestClass.Statics & typeof My.Custom.Namespace.MyInstance>{ // this is necessary to define in `<>` !!!
			MyPublicStaticMethod: () => {
				console.log("My.Custom.Namespace.MyInstance.MyPublicStaticMethod()");
			},
			OTHER_PROP: 'other_default'
		},
		constructor: function (cfg: Ext.Test.TestClass.Cfg) {
			this.other = cfg.other;
		},
		MyPublicInstanceMethod: function () {
			var otherDefault = this.statics().OTHER_PROP;
			console.log(this.other, otherDefault);
		}
	}
);
// my extended instance:
declare namespace My.Custom.Namespace.MyInstance {
	class Extended extends My.Custom.Namespace.MyInstance {
		static MyPublicStaticMethodExtended? (): void;
		otherExtended?: string;
		MyPublicInstanceMethodExtended (): void;
	}
}
Ext.define(
	'My.Custom.Namespace.MyInstance.Extended', 
	<My.Custom.Namespace.MyInstance.Extended & Ext.Base.Def>{
		extend: My.Custom.Namespace.MyInstance.$className,
		statics: <Ext.Test.TestClass.Statics & typeof My.Custom.Namespace.MyInstance.Extended>{ // this is necessary to define in `<>` !!!
			MyPublicStaticMethodExtended: () => {
				console.log("My.Custom.Namespace.MyInstance.Extended.MyPublicStaticMethod");
			},
			ANOTHER_PROP: 'another_default'
		},
		constructor: function (cfg: Ext.Test.TestClass.Cfg) {
			this.other = cfg.other;
		},
		MyPublicInstanceMethodExtended: function () {
			var another = this.statics().ANOTHER_PROP;
			console.log(this.other, another);
		}
	}
);
// creating instances:
var testInstance1 = Ext.create(
	My.Custom.Namespace.MyInstance.$className, 
	<Ext.Test.TestClass.Cfg>{
		other: 'other1'
	}
) as My.Custom.Namespace.MyInstance;
testInstance1.MyPublicInstanceMethod();

var testInstanceExtended = new My.Custom.Namespace.MyInstance.Extended(
	<Ext.Test.TestClass.Cfg>{
		another: 'another2'
	}
);
testInstanceExtended.MyPublicInstanceMethodExtended();

My.Custom.Namespace.MyInstance.MyPublicStaticMethod();
My.Custom.Namespace.MyInstance.Extended.MyPublicStaticMethod();
@tomFlidr tomFlidr added the enhancement New feature or request label Aug 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant