Re:Migrating a 10,000-line legacy JavaScript codebase to Typ...
by peteretep on Friday June 17, 2016 @01:27AM
(#11920842)
Ah, that's a neat idea. I wonder if there's any easy way to then scan your code for type aliases that are `any` to make sure you've caught all of those.
Re:Migrating a 10,000-line legacy JavaScript codebase to Typ...
by chrismbarr on Friday June 17, 2016 @07:49AM
(#11922041)
Yes! We have several 100% TypeScript applications and we've found TSLint to be invaluable for maintaining code quality - http://palantir.github.io/tslint/
You can ban the `any` type along with lots of other rules. You can also write your own custom rules, or implimnet rulesets by 3rd parties like Microsoft: https://github.com/Microsoft/tslint-microsoft-contrib and ESLint: https://github.com/buzinas/tslint-eslint-rules
Re:Migrating a 10,000-line legacy JavaScript codebase to Typ...
by ghh on Friday June 17, 2016 @01:36AM
(#11920865)
Most development setups have a way to do a regex search through a codebase; You'd be looking for declare type.*any
Re:Migrating a 10,000-line legacy JavaScript codebase to Typ...
by wrigby on Friday June 17, 2016 @06:00PM
(#11925817)
If you're IDE doesn't do it, then I highly recommend using ack[1]. It's my go-to tool for tasks like this, and I've found it a joy to use.
Re:Migrating a 10,000-line legacy JavaScript codebase to Typ...
by wvenable on Friday June 17, 2016 @01:31AM
(#11920850)
Probably best to put all the any type aliases in one place then you know what they are all.
Re:Migrating a 10,000-line legacy JavaScript codebase to Typ...
by wldcordeiro on Friday June 17, 2016 @02:23AM
(#11920988)
Common prefix or suffix? I know in the RxJS community it's not uncommon to suffix an Observable variable like `fooBar$`
Re:Migrating a 10,000-line legacy JavaScript codebase to Typ...
by smt88 on Friday June 17, 2016 @02:47AM
(#11921045)
I think that would be more trouble than it's worth. Since "any" is a keyword, you wouldn't even need regex to search for it. Something like this would work:
" = any"
This would only show types that are aliases of the "any" type. As a side note, the following would create a compiler error ("cannot find name 'any'") because types are metadata and can't be used as program data.
const something = any;
Re:Migrating a 10,000-line legacy JavaScript codebase to Typ...
by Sharlin on Friday June 17, 2016 @09:57AM
(#11922639)
Heh, sure does bring memories of the C64 BASIC where string-typed variables and functions were marked with a $ suffix :)
Re:Migrating a 10,000-line legacy JavaScript codebase to Typ...
by jsprogrammer on Friday June 17, 2016 @02:24AM
(#11920990)
Shouldn't my compiler do such things for me?
Re:Migrating a 10,000-line legacy JavaScript codebase to Typ...
by ludamad on Friday June 17, 2016 @02:40AM
(#11921030)
What do you want the compiler to do in this scenario?
Re:Migrating a 10,000-line legacy JavaScript codebase to Typ...
by jsprogrammer on Friday June 17, 2016 @10:35AM
(#11922846)
Infer and track types. Just as was described in the parent post.
Re:Migrating a 10,000-line legacy JavaScript codebase to Typ...
by ehsanu1 on Friday June 17, 2016 @11:57AM
(#11923389)
Both those versions should work the same from the compiler's point of view. But from a programmer's point of view, specifically someone migrating a JS system to TS, it's useful to read "oldVarType" instead of "any", because it tells you that it was just a quick way to migrate rather than necessarily designed to be "any".
Re:Migrating a 10,000-line legacy JavaScript codebase to Typ...
by jsprogrammer on Friday June 17, 2016 @03:06PM
(#11924865)
Yes, and it would be nice if the compiler could produce such an annotated version automatically, instead of the programmer having to use the highly error-prone technique of typing it all out manually.
For a single function it's not a bug deal, but if you are migrating 10k lines, I'd view it as a near necessity.
Re:Migrating a 10,000-line legacy JavaScript codebase to Typ...
by jdmichal on Friday June 17, 2016 @04:49PM
(#11925457)
So you want the compiler to automatically generate alias types for every un-typed variable and parameter? I assume you would also want the compiler to (somehow?) infer types that should be the same type and merge them, since otherwise the first step is kind of silly. I'd argue that that's a pretty difficult task, tantamount to inferring programmer intent.
Re:Migrating a 10,000-line legacy JavaScript codebase to Typ...
by jsprogrammer on Friday June 17, 2016 @05:37PM
(#11925721)
Yes, I want merging, but I imagine there could also be an indication that the merge was automatic and in an IDE setting, I'd expect to be able to easily split out the types, if I want.
That is to introduce a preliminary type definition. Instead of:
You can write: This way, you can signal that it's not just any kind of any, but a particular kind of any, which is now trackable in your codebase.When you're ready, you can gradually update the OldVarType declaration and solve the compiler type check warnings from there. Union types [1] can be quite useful then too.
[1] https://www.typescriptlang.org/docs/handbook/advanced-types....