Skip to content

Auto-initialisation of string literal types in some circumstances #1486

@danielearwicker

Description

@danielearwicker

Regarding issue #1003. Given:

interface A { P: "P"; }

All of the following are valid:

var v1: A = { P: undefined };
var v2: A = { P: null };
var v3: A = { P: <any>5 };
var v4: A = { P: "P" };

But (consistent with TS 1.3) this is not, because it doesn't explicitly initialise P:

var v5: A = { };

I suggest the compiler behave as if P had been initialised, as for v4. This is the overwhelmingly likely option and avoids the redundancy of both stating the type A and repeating the definition of P.

For classes:

class C1 implements A { }
class C2 { P: "P" }
class C3 { P: "P" = "P" }

I suggest for C1 and C2 the compiler behave as for C3, for a similar reason: that's almost certain to be what I'd have to write out by hand.

Metadata

Metadata

Assignees

No one assigned

    Labels

    DeclinedThe issue was declined as something which matches the TypeScript visionOut of ScopeThis idea sits outside of the TypeScript language design constraintsSuggestionAn idea for TypeScript

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions