Giter VIP home page Giter VIP logo

Comments (7)

SamChou19815 avatar SamChou19815 commented on June 26, 2024

This is due to the fact that things like

declare const obj: {foo: string};
declare const key: string;
obj[key]

are never safe, and just returns any before. In the latest Flow version, we choose to start loudly error.

from flow.

gkz avatar gkz commented on June 26, 2024

I tried your initial example, locally over two files, and was not able to reproduce the error.

from flow.

Robloche avatar Robloche commented on June 26, 2024

I struggled a bit but I eventually understood what I did wrong.
Thanks for your answer, @SamChou19815!

from flow.

Robloche avatar Robloche commented on June 26, 2024

Well, there's still something bothering me.
What about this example:

enum MyEnum {
    ValFoo = 'foo',
    ValBar = 'bar'
}

declare const obj: {foo: string, bar: string};
declare const key: string;
obj[MyEnum.ValFoo as string]

Why doesn't it work, since MyEnum.ValFoo as string is indeed the string "foo"?

from flow.

Robloche avatar Robloche commented on June 26, 2024

In fact, I still don't understand the difference between (1):

type Dict<T> = {| [string]: T |};
export type BoolDict = Dict<bool>;

and (2):

export type BoolDict = {| [string]: bool |};

The following code does not work with (1) but is OK with (2):

const bd: BoolDict = { foo: true };
const k = 'foo';
const {[k]: v} = bd;

from flow.

SamChou19815 avatar SamChou19815 commented on June 26, 2024

The enum example is an issue with enum feature. The right solution is to support enum key rather than continue to allow this unsafe access.

In fact, I still don't understand the difference between (1):

type Dict<T> = {| [string]: T |};
export type BoolDict = Dict<bool>;

and (2):

export type BoolDict = {| [string]: bool |};

The following code does not work with (1) but is OK with (2):

const bd: BoolDict = { foo: true };
const k = 'foo';
const {[k]: v} = bd;

I am still unable to reproduce this? Can you provide a try-flow link?

from flow.

Robloche avatar Robloche commented on June 26, 2024

Unfortunately, I can't.
I think it has something to do with export & import and only fails when the code is spread across multiple files, because it works correctly in try-flow.

from flow.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.