Giter Site home page Giter Site logo

typesmith's People

Contributors

sparebytes avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

typesmith's Issues

How to use in a library, how to use inline defined types?

Hi.

Two questions:

(1) In all examples, the type is first defined as a named interface (or a type) and only then its name is passed to assertTypeFn:

interface Person {
  name: string;
}
const assertPerson = assertTypeFn<Person>();
assertPerson(json).unwrap();

Is it intentional that it doesn't work with anonymous types?

const assertPerson = assertTypeFn<{ name: string; }>();  // <-- does not work
assertPerson(json).unwrap();

(2) I haven't found any way to pass the type via a generic parameter to some library function:

// json-request.ts
export async function jsonRequest<TRes>(url: string) {
  const json = await fetch(url); // do some more magic there too, like logging
  const asserter = assertTypeFn<TRes>();
  return asserter(json).unwrap(); // <-- does not work
}

// some-file.ts
import { jsonRequest } from "json-request";
...
const res = await jsonRequest<{ 
  name: string;
  age?: number;
}>("https://example.com");
// I want res here to be (a) strongly typed and (b) validated at run-time

Probably it's expected, because TS transformer works locally and doesn't trace the callers. My question here is - how do I express something similar to this? Because the above syntax (some-file.ts) is basically the essence of what's needed in 99% of the cases... especially in the code which sends lots of different requests, and each request is mentioned there only once (so it makes no sense to create a named type for its response structure).

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.