Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // @flow
- type StrictMapType<V> = {
- [key: string]: V
- };
- const createStrictMap = <V: *>(map: StrictMapType<V>): (name: string) => V => {
- return (name) => {
- if (name in map) {
- return map[name];
- }
- throw new Error();
- };
- };
- const persons = createStrictMap({
- bar: 'BAR',
- foo: 'FOO'
- });
- const foo: string = persons('foo');
- // This triggers Flow error.
- const bar: number = persons('bar');
- // @flow
- type StrictMapType<V> = {
- [key: string]: V
- };
- const createStrictMap = <V: *>(map: StrictMapType<V>) => {
- return new Proxy(
- map,
- {
- get: (subject, name) => {
- if (name in subject) {
- return subject[name];
- }
- throw new Error();
- }
- }
- );
- };
- const persons = createStrictMap({
- bar: 'BAR',
- foo: 'FOO'
- });
- const foo: string = persons.foo;
- // This triggers Flow error.
- const bar: number = persons.bar;
- type StrictMap<T> = T & { [key: string]: any };
- function createStrictMap(map: *): StrictMap<*> {
- return new Proxy(
- map,
- {
- get: (subject, name) => {
- if (name in subject) {
- return subject[name];
- }
- throw new Error();
- }
- }
- );
- };
- const persons = createStrictMap({
- bar: 1,
- foo: 'FOO'
- });
- const foo: string = persons.foo;
- const bar: number = persons.bar;
- const another: number = persons.another;
Add Comment
Please, Sign In to add comment