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

TypeScript Libraries Support #134

Open
NeilKleistGao opened this issue Sep 2, 2022 · 18 comments
Open

TypeScript Libraries Support #134

NeilKleistGao opened this issue Sep 2, 2022 · 18 comments

Comments

@NeilKleistGao
Copy link
Member

NeilKleistGao commented Sep 2, 2022

Implementing ts2mls to allow using TypeScript libraries in MLscript.

Candidate TypeScript Libraries:

@NeilKleistGao
Copy link
Member Author

NeilKleistGao commented Sep 2, 2022

TODO:

  • Generate MLscript declarations in new syntax for classes
  • Generate correct namespace structure by extending namespaces in NewParser.scala

@chengluyu
Copy link
Member

Is it from .ts to .mls, or .d.ts to .mls?

@LPTK
Copy link
Contributor

LPTK commented Sep 8, 2022

I think it should work with both. Right?

@NeilKleistGao
Copy link
Member Author

I think so. I can add some .d.ts tests later.

@NeilKleistGao
Copy link
Member Author

NeilKleistGao commented Oct 3, 2022

  • Generate type alias declaration

@NeilKleistGao
Copy link
Member Author

All interfaces are from TypeScript types.ts

@NeilKleistGao
Copy link
Member Author

  • Add private/protected
class A {
    private a: number
    protected b: string
}

@NeilKleistGao
Copy link
Member Author

  • deal with export in ts

Example:

export namespace N1 {
    interface IF {
        foo()
    }

    class Base {
        a: number = 10
    }

    export class MyClass extends Base implements IF {
        foo() {}
    }
}

In this case, Base will not be exported so users can't access to it. However, we still need to consider that MyClass extends the Base so we need to add a: number into MyClass

@NeilKleistGao
Copy link
Member Author

  • deal with export in ts

Example:

export namespace N1 {
    interface IF {
        foo()
    }

    class Base {
        a: number = 10
    }

    export class MyClass extends Base implements IF {
        foo() {}
    }
}

In this case, Base will not be exported so users can't access to it. However, we still need to consider that MyClass extends the Base so we need to add a: number into MyClass

Also we can create an instance by calling the exported function, but we can't use new:

export namespace N1 {
    class Foo {
        a: number = 10
    }

    export function get(): Foo {
        return new Foo();
    }
}

let x = N1.get()
console.log(x.a)

// error!
// let y = new N1.Foo() 

@NeilKleistGao
Copy link
Member Author

NeilKleistGao commented Oct 11, 2022

  • Fix such situation:
const none: Option<never> = { _tag: 'None' }
const some = <A>(a: A): Option<A> => ({ _tag: 'Some', value: a })

@LPTK
Copy link
Contributor

LPTK commented Oct 11, 2022

Interesting. What's the return type of N1.get() when viewed from the outside of this module/file? If it's something like N1.Foo, then that means we should still export the class, but simply make its constructor private.

@NeilKleistGao
Copy link
Member Author

Interesting. What's the return type of N1.get() when viewed from the outside of this module/file? If it's something like N1.Foo, then that means we should still export the class, but simply make its constructor private.

The type I got is:

symbol: <ref *1> SymbolObject {
      flags: 32,
      escapedName: 'Foo',
      declarations: [Array],
      exports: [Map],
      members: [Map],
      valueDeclaration: [NodeObject],
      parent: undefined,
      isReferenced: 788968,
      id: 14
   },

In the members, we can also get private/protected members.
So I think we do need a private constructor. 🤔

@NeilKleistGao
Copy link
Member Author

I also find something like this in https://github.com/gcanti/fp-ts/blob/master/src/Option.ts.

export const URI = 'Option'

I think we'd better convert all stuffs rather than merely functions or interfaces.

@LPTK
Copy link
Contributor

LPTK commented Oct 14, 2022

Huh, well yeah of course we should convert these too...

@LPTK
Copy link
Contributor

LPTK commented Oct 14, 2022

(Note that the URI machinery is for higher kinded types and is used in conditional types, which we don't yet support.)

@NeilKleistGao
Copy link
Member Author

NeilKleistGao commented Oct 17, 2022

  • Add readonly support(mut in MLScript)

@kevinbarabash
Copy link

Are you planning to support mapped types, conditional types, etc.?

@LPTK
Copy link
Contributor

LPTK commented Apr 25, 2023

@kevinbarabash Yes, the plan is to eventually support arbitrary computations in types (in a way that cleanly generalizes TypeScript's various ad-hoc type computation syntaxes). However, there is a long road before we get there, as for the moment we are busy working out the basic components of the language and type system.

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

No branches or pull requests

4 participants