TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

TypeScript Without Transpilation (2021)

44 点作者 berlam将近 2 年前

12 条评论

franky47将近 2 年前
Sorry for being <i>that</i> guy, but this should probably be called &quot;typechecking without transpilation&quot;.<p>What ends up being written is not TypeScript syntax (which does require transpilation to run).<p>JSDoc is awesome for incremental adoption though, I&#x27;ve migrated a few codebases this way without too much pain.
评论 #37088536 未加载
评论 #37088350 未加载
a-ungurianu将近 2 年前
JSDoc can get you pretty far, but it can be clumsy sometimes. There’s a [TC39 proposal](<a href="https:&#x2F;&#x2F;github.com&#x2F;tc39&#x2F;proposal-type-annotations">https:&#x2F;&#x2F;github.com&#x2F;tc39&#x2F;proposal-type-annotations</a>) to allow types to live in JS code and be treated as comments (similar with Python types today)
评论 #37088585 未加载
stevebmark将近 2 年前
JSDoc comments are definitely <i>not</i> the “best of both worlds,” they are a limited and verbose subset of typing. I would only use them for specific reasons, like I’m in a hostile ecosystem that dogmatically doesn’t want any build steps (like WebGL&#x2F;WebGPU engines).
评论 #37091043 未加载
afavour将近 2 年前
I&#x27;ve found a happy medium to be annotating functions etc with JSDoc style comments but keeping interfaces etc. in a .d.ts file. The Typescript compiler is able to import it from a .js file just fine.
评论 #37091996 未加载
评论 #37091193 未加载
评论 #37089337 未加载
eyelidlessness将近 2 年前
Some of the limitations listed are wrong:<p>&gt; The main limitation is that you don&#x27;t have access to some TypeScript-specific syntax.<p>&gt; * as, also known as type assertions (or casts)<p>You generally shouldn’t (as the article notes), but you can:<p><pre><code> const foo = &#x2F;** @type {Bar} *&#x2F; ({ something: { satisfying: &#x27;Bar&#x27;, }, }); </code></pre> Note: the parentheses are necessary, they <i>are</i> the JS&#x2F;JSDoc mechanism for type casts.<p>This also works for `as const`, which is often better when you can use readonly types:<p><pre><code> const foo = &#x2F;** @type {const} *&#x2F; ({ something: &#x27;else&#x27;, }); &#x2F;&#x2F; { readonly something: &#x27;else&#x27; } </code></pre> Better still, the satisfies operator also works (its JSDoc support lagged a bit though):<p><pre><code> &#x2F;** @satisfies {Bar} *&#x2F; const foo = …; </code></pre> This will infer the type of `foo` <i>and</i> check it for assignability to `Bar`.<p>&gt; * is, also known as type predicates<p>This definitely works:<p><pre><code> &#x2F;** * @param {unknown} value * @return {value is Foo} *&#x2F; const isFoo = (value) =&gt; … </code></pre> You can also define the guard as a standalone&#x2F;assignable type:<p><pre><code> &#x2F;** * @callback IsFoo * @param {unknown} value * @return {value is Foo} *&#x2F; </code></pre> The @callback tag is ~equivalent to a type alias for a function signature.<p>Also useful, if needed, the @template tag which is roughly equivalent to TS type parameters (generics) which you can use, for example, to assign generic type guards:<p><pre><code> &#x2F;** * @template T * @callback IsT * @param {unknown} value * @return {value is T} *&#x2F; &#x2F;** @type {IsT&lt;Foo&gt;} *&#x2F; const isFoo = (value) =&gt; … </code></pre> [Disclaimer: typed this comment on my phone from memory, apologies for any typos or mistakes!]
Ciantic将近 2 年前
I&#x27;m using this setup at the moment, mostly because I work on legacy code base that has no good types yet.<p>Typically you would do jsconfig.json though, which is same as tsconfig.json but allowJs is already set. Mostly stylistic change, but some tooling might benefit for having jsconfig instead of tsconfig.
评论 #37089257 未加载
oefrha将近 2 年前
&#x2F;&#x2F; @ts-check works until you need to do an assertion, a cast, a predicate, pass an explicit type to a generic method, etc., then it gets very annoying. I only ever use it if I’m writing a single self-contained and relatively short script and can’t be bothered with a build step.
jchanimal将近 2 年前
Does anyone know if I have a TypeScript project, is there an automated process to turn it into this format this article describes? Eject for TS, I guess.
评论 #37090807 未加载
评论 #37091117 未加载
评论 #37088330 未加载
throw_m239339将近 2 年前
This is exactly how I use typescript, as a linter and static type analyzer for javascript. Unfortunately, typescript support for JSdoc is quite basic. So that method has its limit. But I&#x27;m not committing any typescript code.<p>Someone here just suggested to import type definition files with JS doc annotations, and that&#x27;s actually a good idea.
mickael-kerjean将近 2 年前
Right on time as I&#x27;ve been trying to add some type safety onto my OSS project. Does anyone have some extra info on how to best leverage JSDoc to write safe(r) JS code?
hliyan将近 2 年前
I thought this was going to be a project like ts-node [1]<p>[1] <a href="https:&#x2F;&#x2F;github.com&#x2F;TypeStrong&#x2F;ts-node">https:&#x2F;&#x2F;github.com&#x2F;TypeStrong&#x2F;ts-node</a>
tantalor将近 2 年前
Confused about the mentions of Elm. What does this have to do with Elm?
评论 #37089128 未加载
评论 #37089044 未加载