Skip to content

Can we define a ReadOnly interface directly? #17769

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
zheeeng opened this issue Aug 14, 2017 · 3 comments
Closed

Can we define a ReadOnly interface directly? #17769

zheeeng opened this issue Aug 14, 2017 · 3 comments
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript

Comments

@zheeeng
Copy link

zheeeng commented Aug 14, 2017

TypeScript Version: 2.4.0 / nightly (2.5.0-dev.201xxxxx)

Code
There three ways we can define a readonly type or interface.

// First
type ReadonlyState = Readonly<{
  foo: string,
  bar: string,
}>

// Second
interface ReadonlyState2 {
  readonly foo: string,
  readonly bar: string,
}

// Third
interface State3 {
  foo: string,
  bar: string,
}

type ReadonlyState3 = Readonly<State3>

The first one we can get the readonly type directly and it's easy to writing.
The second one, we can get the readonly interface, but if there are more properties we have to repeatedly declare its readonly modifier.
The third one, we can get the readonly interface directly, it assign a non-readonly interface to a type. And the State3 seems should be eliminated.

Whether we have a way to define a readonly interface just like the first way does?

I expect to define a React State interface directly which is readonly.

@RyanCavanaugh RyanCavanaugh added In Discussion Not yet reached consensus Suggestion An idea for TypeScript labels Aug 16, 2017
@RyanCavanaugh
Copy link
Member

One might imagine it being possible to write

readonly interface Foo {
  a: number;
}

@mhegazy
Copy link
Contributor

mhegazy commented Nov 7, 2017

We have discussed this in the past, and felt the right approach here should be readonly type operator.. this is tracked by #10725

@mhegazy
Copy link
Contributor

mhegazy commented Nov 7, 2017

closing in favor of #10725

@mhegazy mhegazy closed this as completed Nov 7, 2017
@microsoft microsoft locked and limited conversation to collaborators Jun 14, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
In Discussion Not yet reached consensus Suggestion An idea for TypeScript
Projects
None yet
Development

No branches or pull requests

3 participants