Skip to content
This repository has been archived by the owner on Feb 16, 2021. It is now read-only.

type unknow - error not checked #25

Closed
freddi301 opened this issue Feb 23, 2017 · 5 comments
Closed

type unknow - error not checked #25

freddi301 opened this issue Feb 23, 2017 · 5 comments
Labels

Comments

@freddi301
Copy link

type getTotalsP = {| from: Date, to: Date, accountId: string |};

async function getTotals({ from, to, accountId }: getTotalsP) {
  const orders = await Order.find({
    _id: accountId,
    startTime: { $gte: from, $lte: to },
  }, 'status');
  return orders;
}

import * as t from 'flow-io/src/index.js';

const TotalReq = t.$exact({
  accountId: t.string,
  from: t.instanceOf(Array),
  to: t.instanceOf(Date),
});

router.post('/total', awrapres(req => {
  const params : getTotalsP = t.fromValidation(req.body, TotalReq);
  return getTotals(params);
}));
@gcanti
Copy link
Owner

gcanti commented Feb 24, 2017

Not sure what's the issue here, could you please provide a minimal runnable example reproducing the problem?

> flow

src/index.js:6
  6:   const orders = await Order.find({
                            ^^^^^ identifier `Order`. Could not resolve name

src/index.js:21
 21: router.post('/total', awrapres(req => {
     ^^^^^^ identifier `router`. Could not resolve name

src/index.js:21
 21: router.post('/total', awrapres(req => {
                           ^^^^^^^^ identifier `awrapres`. Could not resolve name

@freddi301
Copy link
Author

Dont need to run it, just check it with flow

/* @flow */

type getTotalsP = {| from: Date, to: Date, accountId: string |};

async function getTotals({ from, to, accountId }: getTotalsP) {
  return 10000;
}

import * as t from 'flow-io/src/index.js';

const TotalReq = t.$exact({
  accountId: t.string,
  from: t.instanceOf(Array),
  to: t.instanceOf(Date),
});

// using type_at_pos here you get type unknown
// but using https://github.com/andreypopp/validated in similar case you get the right type
const params2 = t.fromValidation(({}: any), TotalReq);

// here the schema type does not match return type (should be error)
// {| from: Date, to: Date, accountId: string |} != {| from: Array, to: Date, accountId: string |}
const params: getTotalsP = t.fromValidation(({}: any), TotalReq);
getTotals(params);

Anyway, nice project 👍 I will use it at work when the issue is solved.

@gcanti
Copy link
Owner

gcanti commented Feb 24, 2017

using type_at_pos here you get type unknown

Seems a bug of type-at-pos

// @flow

import * as t from 'flow-io';

// type-at-pos says correctly ArrayType<Array<string>>
const A = t.array(t.string)

// type-at-pos says (unknown)
const TotalReq = t.object({
  accountId: t.string,
  from: t.instanceOf(Array),
  to: t.instanceOf(Date),
});

// but...

const x = t.fromValidation({}, TotalReq);

// `flow check` correctly shows an error: property `a`. Property not found in object type
x.a

// `flow check` correctly shows no errors
;(x.accountId: string)
;(x.from: Date)

here the schema type does not match return type (should be error)

Relevant #11

Btw bugs like these make flow-io hardly usable, that's why I switched to TypeScript https://github.com/gcanti/io-ts

@freddi301
Copy link
Author

unrelated: today, which is better, typescript or flow? i knew that typescript were less intelligent (maybe things changed)

@freddi301
Copy link
Author

Hi, i got a workaround https://github.com/freddi301/flow-validator

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

No branches or pull requests

2 participants