Skip to content
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

Dash in interface key causes error #4736

Open
adius opened this issue Aug 29, 2017 · 9 comments
Open

Dash in interface key causes error #4736

adius opened this issue Aug 29, 2017 · 9 comments

Comments

@adius
Copy link

adius commented Aug 29, 2017

export interface Header {
  Authorization: string,
  'User-Agent': string,
}
$ flow
Error: source/types.js:31
 31:   'User-Agent': string,
       ^^^^^^^^^^^^ illegal name

Found 1 error

Is this a bug, or is there something I can do to fix it?

@asolove
Copy link
Contributor

asolove commented Aug 29, 2017

It looks like string literals are never allowed at this position. Presumably because they can only be looked up by the dynamic property syntax (a["user-agent"]) rather than as static property access with .. Flow cant do much to type that.

@jcready
Copy link
Contributor

jcready commented Aug 29, 2017

@asolove that seems like terrible reasoning. For instance you can do this and everything passes:

type Header = {
  Authorization: string,
  ['User-Agent']: string,
}

const a: Header = {
  Authorization: '',
  'User-Agent': '',
}

So clearly flow is able to type check dynamic/string-literal properties.

@asolove
Copy link
Contributor

asolove commented Aug 29, 2017

But this also type checks:

type Header = {
  Authorization: string,
  ['User-Agent']: string,
}

const a: Header = {
  Authorization: ''
}

Which I think is strong evidence that the bracket syntax doesn't work in type declarations.

@vkurchatkin
Copy link
Contributor

Which I think is strong evidence that the bracket syntax doesn't work in type declarations.

It DOES work, but it just add indexer, not a property.

@jcready
Copy link
Contributor

jcready commented Aug 29, 2017

That's because putting 'User-Agent' in the square brackets makes it an indexer instead of just a property. My point was that flow is able to type check properties of objects which contain hyphens when you use an indexer, so it should be able to do that same type checking without using an indexer and just using a string literal property instead.

@vkurchatkin
Copy link
Contributor

@adius I recommend you to use type instead of interface:

export type Header = {
  Authorization: string,
  'User-Agent': string,
}

@adius
Copy link
Author

adius commented Sep 3, 2017

@vkurchatkin What are the advantages?

@vkurchatkin
Copy link
Contributor

@adius well, one advantage is that it actually works

@goodmind goodmind changed the title Dash in object key causes error Dash in interface key causes error Jul 22, 2019
@EdmundsEcho
Copy link

EdmundsEcho commented Jul 21, 2020

I'm slowly integrating flow into my project. I use static type checking in several other contexts and am eager to make it more so with my create-react-app UI.

Unfortunately, I'm not in a position to rename my props. For better or worse, we support the use of hyphenated names. The inability to use a hyphenated name in the interface declaration seems like a meaningful shortcoming... types are to communicate intent, working-around this issue will get in the way of this communication.

I understand that the use of [] encodes an index... not a way to reference a hyphenated name.

Are there plans to figure out a solution here? Is this considered a relatively important issue? Or is there a reasonable work-around that is currently the norm for this situation?

Thanks in advance!

Updated

Based on the scope of the implements keyword being for class and not type, I can see how this question only makes sense if we expand the use of the keyword accordingly (because a class declaration does not support hyphenated names... that makes sense). So, the scope of the requested input likely has to include the future of using implements for the type keyword.

Final note, this feature would be consistent with how a graphql spec works.

- E

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants