Nswag: TypeScript change <any> to as any

Created on 10 Sep 2018  ·  12Comments  ·  Source: RicoSuter/NSwag

TypeScript is moving away from casting via the bracket syntax in favor of the as syntax, primarily for tsx purposes. With babel7 now supporting typescript, but with no support for the bracket casting syntax, I believe it makes sense to move away from this syntax in favor of as.

help wanted enhancement

Most helpful comment

It goes back to 1.6 so it should be safe to implement.

https://www.typescriptlang.org/docs/handbook/release-notes/typescript-1-6.html

All 12 comments

With babel7 now supporting typescript, but with no support for the bracket casting syntax

How is it supporting TypeScript if it's missing that?

Unfortunately that is a very good question.

https://blogs.msdn.microsoft.com/typescript/2018/08/27/typescript-and-babel-7/

There are more than a few features that it outright does not support. The only one that nswag seems to break for is casting. Also things like async generators which still need babel plugins to support, but work out of the box for TypeScript. I would rate the support as "piss poor", but react native 57+ will use it exclusively.

My question: Is the as operator supported in all ts versions?

It goes back to 1.6 so it should be safe to implement.

https://www.typescriptlang.org/docs/handbook/release-notes/typescript-1-6.html

@RSuter can I take this issue and modify the angular template first ?

image

tslint raises warnings because of this

Due to this issue, it is no longer possible to use NSwag with the latest version of create-react-app and TypeScript. Or is there any workaround?

So this is only a problem with babel’s typescript implementation? Documented here: https://devblogs.microsoft.com/typescript/typescript-and-babel-7/

The latest version of create-react-app has TypeScript support built-in based on Babel (whereas before, you would use a now-deprecated fork of create-react-app that actually used the standard TypeScript compiler). Since NSwag generates <any>null casts, for example, the generated clients don't compile anymore.

Ok, i think it should be quite simple to replace all those casts to “as any” casts in all templates...

It seems that the as operator has been introduced in ts 1.6 - so this should not be a breaking change for any users.

https://github.com/Microsoft/TypeScript/wiki/What's-new-in-TypeScript#new-tsx-file-extension-and-as-operator

Hold on a second... it seems this is now actually supported: https://babeljs.io/docs/en/babel-preset-typescript

It didn't work when I tried it out as I used a .tsx extension, where these casts are not supported even by the TypeScript compiler. With a .ts extension, it works with the latest create-react-app. Sorry about that!

Was this page helpful?
0 / 5 - 0 ratings