Microsoft/TypeScript

Unable to use Symbol as key for jsdoc `@typedef`

Open

#47,259 创建于 2021年12月28日

在 GitHub 查看
 (4 评论) (0 反应) (0 负责人)TypeScript (6,726 fork)batch import
BugDomain: JSDocHelp Wanted

仓库指标

Star
 (48,455 star)
PR 合并指标
 (平均合并 6天 17小时) (30 天内合并 9 个 PR)

描述

Bug Report

🔎 Search Terms

jsdoc ts2454

🕗 Version & Regression Information

  • This is the behavior in every version I tried, and I reviewed the FAQ for entries about 2454

⏯ Playground Link

Playground link with relevant code

💻 Code

class Foo {
    someProp = "value";
}

const sym = Symbol("sym");
/** @typedef {Foo & { [sym]?: string }} FooWithSym */
//                     ^^^----- Variable 'sym' is used before being assigned (2454)

Note that this error only happens with strictNullChecks enabled.

🙁 Actual behavior

ts2454

🙂 Expected behavior

No errors

When using TypeScript rather than JavaScript, this error does not occur.

Here is the TypeScript equivalent: (playground)

// Normally this would be an imported class
class Foo {
    someProp = "value";
}

// add the symbol type to Foo
const sym = Symbol("sym");
type FooWithSym = Foo & {
    [sym]?: string;
}

const withoutSym = new Foo();
withoutSym[sym] = "test"; // error, as expected


const withSym : FooWithSym = new Foo();
withSym[sym] = "test"; // this works

const y = withSym[sym];
console.log(y); // y is `string | undefined`

贡献者指南