Skip to content

TypeScript Standalone #3

Merged
ollieatkinson merged 10 commits intothousandyears:trunkfrom
tomsugarev:TypeScript-generator
Aug 12, 2022
Merged

TypeScript Standalone #3
ollieatkinson merged 10 commits intothousandyears:trunkfrom
tomsugarev:TypeScript-generator

Conversation

@tomsugarev
Copy link
Copy Markdown
Contributor

@tomsugarev tomsugarev commented May 27, 2022

image

Typescript generator

Bit of extra fiddling compared to Swift and Kotlin.

Looking at the L_test_one class in TS it needs to explicitly state the good property.
In Swift/Kotlin it doesn't. Guess this is a difference in the protocol(interface) inheritance??

class L_test_one extends L implements I_test_one {
  good!: L_test_type_odd_good;
  more = new L_test_one_more(`${this.identifier}.more`);
}
interface I_test_one extends I_test_type_odd {
  more: L_test_one_more;
}
public final class L_test_one: L, I_test_one {
	public override class var localized: String { NSLocalizedString("test.one", comment: "") }
}
public protocol I_test_one: I_test_type_odd {}
public extension I_test_one {
	var `more`: L_test_one_more { .init("\(__).more") }
}

Copy link
Copy Markdown
Member

@steprescott steprescott left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great stuff Tom. Thanks for your contribution.

Comment thread Package.swift Outdated
@steprescott
Copy link
Copy Markdown
Member

Could you also add your generator to the list of generators found here.

Comment thread Sources/TypeScriptStandAlone/Generator.swift Outdated
Comment thread Sources/TypeScriptStandAlone/Generator.swift Outdated
@ollieatkinson
Copy link
Copy Markdown
Member

@tomsugarev I had a go at playing with the generated TS code, looks really good - I have a couple of small suggestions:

  1. Remove localized as we don't use it in TS, unless you can think of a reason we need it. If so, we should make it static to avoid collisions
  2. Make __ protected so you can't access outside of the scope
  3. Create a symbol 'identifier' to be used with subscript syntax
  4. Provide toStringTag symbol so printing the object will also neatly print the string id: [object test.one]
  5. Use I_ types in the generated interfaces

This makes the auto-complete super clean:
image

Access to the identifier can be done through subscript:

test.one['identifier'] // "test.one"

Example Generated Code

interface I { }

 // L
 class L implements I {

   protected __: string;

   constructor(id: string) {
     this.__ = id;
   }

    get [Symbol.toStringTag]() {
        return this.__;
    }

    get ['identifier']() {
        return this.__;
    }
 }

 // MARK: generated types
 class L_test extends L implements I_test {
   one = new L_test_one(`${this.__}.one`);
   two = new L_test_two(`${this.__}.two`);
   type = new L_test_type(`${this.__}.type`);
 }
 interface I_test extends I {
   one: I_test_one;
   two: I_test_two;
   type: I_test_type;
 }
 class L_test_one extends L implements I_test_one {
   good!: L_test_type_odd_good;
   more = new L_test_one_more(`${this.__}}.more`);
 }
 interface I_test_one extends I_test_type_odd {
   more: I_test_one_more;
 }
 class L_test_one_more extends L implements I_test_one_more {
   time = new L_test_one_more_time(`${this.__}}.time`);
 }
 interface I_test_one_more extends I {
   time: I_test_one_more_time;
 }
 class L_test_one_more_time extends L implements I_test_one_more_time {
   one!: L_test_one;
   two!: L_test_two;
   type!: L_test_type;
 }
 type I_test_one_more_time = I_test;
 class L_test_two extends L implements I_test_two {
   no!: L_test_type_even_no;
   bad!: L_test_type_even_bad;
   timing = new L_test_two_timing(`${this.__}}.timing`);
 }
 interface I_test_two extends I_test_type_even {
   timing: I_test_two_timing;
 }
 class L_test_two_timing extends L implements I_test_two_timing {
 }
 type I_test_two_timing = I;
 class L_test_type extends L implements I_test_type {
   even = new L_test_type_even(`${this.__}}.even`);
   odd = new L_test_type_odd(`${this.__}}.odd`);
 }
 interface I_test_type extends I {
   even: I_test_type_even;
   odd: I_test_type_odd;
 }
 class L_test_type_even extends L implements I_test_type_even {
   no = new L_test_type_even_no(`${this.__}}.no`);
   bad = this.no.good;
 }
 interface I_test_type_even extends I {
   no: I_test_type_even_no;
 }
 type L_test_type_even_bad = L_test_type_even_no_good
 class L_test_type_even_no extends L implements I_test_type_even_no {
   good = new L_test_type_even_no_good(`${this.__}}.good`);
 }
 interface I_test_type_even_no extends I {
   good: I_test_type_even_no_good;
 }
 class L_test_type_even_no_good extends L implements I_test_type_even_no_good {
 }
 type I_test_type_even_no_good = I;
 class L_test_type_odd extends L implements I_test_type_odd {
   good = new L_test_type_odd_good(`${this.__}}.good`);
 }
 interface I_test_type_odd extends I {
   good: I_test_type_odd_good;
 }
 class L_test_type_odd_good extends L implements I_test_type_odd_good {
 }
 type I_test_type_odd_good = I;
 const test = new L_test("test");

@ollieatkinson
Copy link
Copy Markdown
Member

Copy link
Copy Markdown
Member

@ollieatkinson ollieatkinson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome @tomsugarev - let's get this in the next release of MindFlare

@ollieatkinson ollieatkinson merged commit ad87202 into thousandyears:trunk Aug 12, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants