1

Iv'e read alot of the Typescript github issues regarding implementation but there's little documentation or people working in this area.

My goal is to basically have typescript understand that..

[1,2,3,4,5].filter(_ % 2).map(_ + 2)
[{name: "test"}, {name: "test2"}].map(_.name).map(_.toUpperCase())

is the same as...

[1,2,3,4,5].filter((x) => x % 2).map((x) => x + 2)
[{name: "test"}, {name: "test2"}].map((x) => x.name).map((x) => x.toUpperCase())

i either want to define a SweetJS macro to convert all underscores to (x) => x at compile time or (peferably) have typescript apply that transformation for me since SweetJS is fairly hacky.

(For reference https://github.com/sweet-js/sweet-core)

How would i go about implementing something that allows typescript to at the bare minimum understand underscore syntax in the correct context \AND\ if possible transform underscores at compile time.

EDIT3: (Edit3 is here for readability) this is how far i have got with the problem, i have opted not to change the compiler to think it is a different type but instead do this....

declare const _: (<T>(arg: T) => T) & { [key: string]: () => any }
const test = [{name: "Shanon", age: 24}].filter(_.age);

which typescript is now happy with, but it is not typesafe so any help would be appreciated.

EDIT: With my minimal compiler skills, i think that i need typescript to understand that when i am within the context of a "CallExpression" underscore has a very unique meaning (CallExpression in the AST).

EDIT2: enter image description here this is the AST defintiion i need outputted (something like this) it's important to note, i don't want the underscore converted before it is typechecked but i want to controle how a underscore is type checked. It should only treat the underscore as this type inside a CallExpression AST type

Shanon Jackson
  • 5,873
  • 1
  • 19
  • 39
  • This would not just be an emit transformation (for which there is support), it would have to be a code transformation that happens before type checking occurres, which is not supported... You would be changing the semantics of the language and you would need to change the compiler at a deeper level. I have played with such things in the past and while fun,I would not recommend you do something like that in production code. – Titian Cernicova-Dragomir May 01 '18 at 03:25
  • not neccessarily, it would not transform the underscore before type checking it would just type check the underscore as its own syntax I.E .filter(_.name !== "") should type the underscore as the AST type "ArrowFunctionExpression" i believe. According to (https://astexplorer.net/) – Shanon Jackson May 01 '18 at 03:53
  • Ok, still extensive compiler work in a part of the compiler not meant for extension. Making `_.name!==""` be interpreted as an `ArrowFunctionExpression` would require changing the parsing stage of the compiler. – Titian Cernicova-Dragomir May 01 '18 at 04:05
  • Possible or impossible? Or possible but 2weeks+ work? and insane compiler knowledge – Shanon Jackson May 01 '18 at 04:08
  • 1
    I believe it would take me 2-3 days to implement it, but I already have extensive knowledge of the compiler. Also it basically requires forking the compiler and you would have to rebase your fork in each release and you would be exposed to breaking changes in complier implementation. So possible, but I would not go there. – Titian Cernicova-Dragomir May 01 '18 at 04:11
  • Thanks for all the advice man really appreciate it didn't have much hope in stackoverflow for a question like this. Guess i'll give up on my underscore dreams until the allow compiler plugins for the parser stage or something? still a compiler rookie. Thanks so much upvoted. – Shanon Jackson May 01 '18 at 04:13
  • Hello @TitianCernicova-Dragomir iv'e added a "EDIT3" in original post for how progress would made to solve this problem, any advice from this point out would be greatly appreciated. Editing the compiler is a no-go so iv'e written the code excerpt in EDIT3 to ALMOST solve this problem (problem now is typesafty) – Shanon Jackson May 01 '18 at 05:16
  • For just 3 extra caracters you don't need to do anything: `const test = [{name: "Shanon", age: 24}].filter(_=>_.age);` I feel the problem you are trying to solve was there when we didn't have arrow functions, right now if you just type `_=>` yo you get everything you need. – Titian Cernicova-Dragomir May 01 '18 at 06:44
  • The problem i'm trying to solve is partial exploration to see if i can create Scala syntax in javascript and have syntactic sugar for arrow functions in higher order functions whilest keeping type safty – Shanon Jackson May 01 '18 at 07:46

0 Answers0