clutz icon indicating copy to clipboard operation
clutz copied to clipboard

Generate literal types for initialized constants

Open jermowery opened this issue 7 years ago • 2 comments

Have clutz generate out strict types for @const annotated fields.

For example:

/** @public {string} @const */
Foo.BAR = "baz";`

Should become:

static readonly BAR: 'baz'

in the generated declarations instead of the current behavior of:

static readonly BAR: string

This can help with type inferencing and the creation of string union types

jermowery avatar Oct 01 '18 19:10 jermowery

I think the term is literal types.

I'm not sure this is a good idea though. Closure Compiler has no concept of literal types, there is no type 'baz' for Closure Compiler. If we'd try to be clever and invent them, we're prone to break other guarantees in the system, e.g. subtyping when such fields are involved. Generally speaking I think we should only produce .d.ts that map Closure's type system into TS, without trying to add precision/fanciness that doesn't exist in Closure.

/CC @rkirov might have an opinion.

mprobst avatar Oct 02 '18 07:10 mprobst

I looked briefly at changing this behavior, it requires using Node rather than JsType, but the function that emits the type is used in several places which will need to be updated, as well as their callers, there are many cascading things :confused:

Goodwine avatar Oct 02 '18 18:10 Goodwine