microsoft / TypeScript

TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
https://www.typescriptlang.org
Apache License 2.0
100.08k stars 12.37k forks source link

Change Diagnostic.code to 'string | number' #28911

Open ajafff opened 5 years ago

ajafff commented 5 years ago

I'm writing a LanguageService plugin for TypeScript that returns custom diagnostics. A Diagnostic needs to have a code property of type number.

I don't have a number. Instead I can provide a string that represents the name of the rule producing the diagnostic. In addition a random number I would choose looks quite ugly in the tooltip VSCode displays for the Diagnostic.

I know TypeScript only ever creates Diagnostics with numeric code, but it's too limiting for plugins. I looked it up and the LanguageServer protocol specifies it as code?: number | string.

My current workaround is to use a type assertion. Until now nothing bad has happened.

weswigham commented 5 years ago

@DanielRosenwasser I wholeheartedly agree with this - I've made it twice in PRs we never merged about extensibility - it just makes a lot of sense for diagnostics that aren't from us.

DanielRosenwasser commented 5 years ago

Seems pretty reasonable to me.

ajafff commented 5 years ago

Continuing discussion from #28959

it might be better to return a new property on Diagnostic objects.

Does this only affect Diagnostic (DiagnosticRelatedInformation to be precise) or should it also be added to DiagnosticMessageChain and/or DiagnosticMessage?

Does this require a change to the LanguageServerProtocol spec?