Typescript: Allow property (dotted) access syntax for types

Created on 16 Dec 2016  路  3Comments  路  Source: microsoft/TypeScript

This works:

interface Test {
    A: 'a' | 'b' | 'c'
}

function test(a: Test['A']) {
}
test('a')

Would be nice if we could use it as that:

function test2(a: Test.A) {
}
test2('a')
Question

Most helpful comment

You can do it by declaring a namespace:

interface Test {
    A: 'a' | 'b' | 'c'
}
namespace Test {
    export type A = Test['A'];
}

function test(a: Test.A) {
}
test('a')

You're effectively proposing that we do this automatically. But, because you can already do it today, it would be a breaking change to do it automatically because we'd potentially conflict with code such as the example.

All 3 comments

You can do it by declaring a namespace:

interface Test {
    A: 'a' | 'b' | 'c'
}
namespace Test {
    export type A = Test['A'];
}

function test(a: Test.A) {
}
test('a')

You're effectively proposing that we do this automatically. But, because you can already do it today, it would be a breaking change to do it automatically because we'd potentially conflict with code such as the example.

Thank you @ahejlsberg
Yes - I was hoping that it would do this automatically but I understand the implication.
Maybe it could throw an error only if it detected such conflict

There's a pretty similar question & example here.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kyasbal-1994 picture kyasbal-1994  路  3Comments

bgrieder picture bgrieder  路  3Comments

manekinekko picture manekinekko  路  3Comments

siddjain picture siddjain  路  3Comments

Zlatkovsky picture Zlatkovsky  路  3Comments