Giter Site home page Giter Site logo

sthir's Introduction

Sthir

The place where TypeScript gets leveraged like nowhere else.

Packages

sthir's People

Contributors

andarist avatar devanshj avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

Forkers

yuth andarist

sthir's Issues

Evaluate the iife produced by `pa` macro when used with `p`

import { p, pa } from "@sthir/macro"

pa(x, p("!==", undefined))

Gets transformed to...

(t => t !== undefined)(x)

This is correct because pa could have even be used with some other predicate not just p...

pa(x, somePredicate)

But in cases where it's used with p the iife can be further evaluated....

x !== undefined

Remove redundant intersections

One'd find many redudant intersections in the narrowed type, eg { a: number, b: string } & { a: number }, these should be removed.

Apart for the fact it would lead to better-looking types, it'd also preserve heuristics that typescript applies to types, for instance the following code would compile if we remove the redundant & { 0: number }. Although of course, it should have compiled with the redundant & { 0: number } too.

import { p, pa } from "@sthir/predicate"

let x = {} as [number, string] | [string]
if (pa(x, p(".0 typeof ===", "number"))) {
  const [_, ...y] = x;
//                  ^? [number, string] & { 0: number }
  let test: [string] = y
//    ~~~~
// Type '(string | number)[]' is not assignable to type '[string]'
}

`accumulateErrors`

/**
 * Creates a new parser that accumulates
 * and yields the errors of the parser
 * passed in a tree-like format,
 * something like this...
 * ```
 * is not of type 'A' as
 *   it is missing key 'x'
 *   it's value at key 'y' is not of type 'B' as
 *     it is missing key 'p'
 *     it's value at key 'q' is not type 'string'
 *   it's value at key 'z' is not type 'number'
 * ```
 */
declare const accumulateErrors:
  <T extends UnknownParser>(p: T) => T

Nested `?` doesn't narrow

import { p, pa } from "@sthir/predicate"

declare let x: { a: { b: string } | undefined }

if (pa(x, p(".a?.b"))) {
  x.a.b // should compile, doesn't
}

Originally posted by @VinSpee in #1 (comment)

here's a minimal repro:

https://tsplay.dev/N7KL4N

import { p } from "@sthir/predicate";

interface Image {
  height: number;
  width: number;
  url: string;
  name: string;
}

interface Variants {
  "square-small2x": Image;
  "square-small": Image;
  "landscape-crop": Image;
  "landscape-crop2x": Image;
}

export type Profile = {
  id: string;
  images: Variants | null | undefined;
  displayName: string;
  jobTitle: string;
};

const data: Profile[] = [
  {
    id: "618c4a28-925c-42df-a655-30552ea53607",
    images: {
      "square-small2x": {
        height: 480,
        width: 480,
        url: "https://avatars.dicebear.com/api/adventurer-neutral/ts.svg",
        name: "square-small2x",
      },
      "square-small": {
        height: 240,
        width: 240,
        url: "https://avatars.dicebear.com/api/adventurer-neutral/ts.svg",
        name: "square-small",
      },
      "landscape-crop": {
        height: 267,
        width: 400,
        url: "https://avatars.dicebear.com/api/adventurer-neutral/ts.svg",
        name: "landscape-crop",
      },
      "landscape-crop2x": {
        height: 533,
        width: 800,
        url: "https://avatars.dicebear.com/api/adventurer-neutral/ts.svg",
        name: "landscape-crop2x",
      },
    },
    displayName: "Jennifer Smith",
    jobTitle: "Marketing Executive",
  },
];

data.filter(p("?.images?.square-small2x"));

Expression produces a union type that is too complex to represent.

Argument of type '["?.images?.square-small2x"]' is not assignable to parameter of type '[] | [Comparator] | [Operator]'.
Type '["?.images?.square-small2x"]' is not assignable to type '[Comparator]'.
Type '"?.images?.square-small2x"' is not assignable to type 'Comparator'.

Oops fix that one type error on main

No idea how this went unnoticed :P

tests/index.test.ts:11:7 - error TS2345: Argument of type '["&", 2]' is not assignable to parameter of type 'PArgsNarrowed<[] | [Comparator<T>] | [Operator<number>]>'.
  Type '["&", 2]' is not assignable to type '[Operator<number>]'.
    Source has 2 element(s) but target allows only 1.

11     p("&", 0b10)
         ~~~~~~~~~

twoslash-tester crashes

$ cd packages/predicate
$ node ../../twoslash-tester/generate tests/types.twoslash-test.ts
generating... E:\sthir\node_modules\typescript\lib\typescript.js:10384
        return sourceFile.getPositionOfLineAndCharacter ?
                          ^

TypeError: Cannot read property 'getPositionOfLineAndCharacter' of undefined
    at Object.getPositionOfLineAndCharacter (E:\sthir\node_modules\typescript\lib\typescript.js:10384:27)
    at E:\sthir\node_modules\@typescript\twoslash\dist\twoslash.cjs.development.js:681:25
    at Array.map (<anonymous>)
    at _loop3 (E:\sthir\node_modules\@typescript\twoslash\dist\twoslash.cjs.development.js:679:40)
    at twoslasher (E:\sthir\node_modules\@typescript\twoslash\dist\twoslash.cjs.development.js:743:17)
    at generate (E:\sthir\twoslash-tester\generate.js:19:48)

Optimize `pm` macro

import { pm } from "@sthir/predicate/macro"

pm(x => f(x) ? [x] : []);

tranforms to

(t => (x => f(x) ? [x] : [])(t).length === 1);

but it could be optimized to

f;

Build failing due to parsing error

$ cd packages/number
$ yarn build
[...]
🎁 error /workspace/sthir/packages/number/src/types.ts: Unexpected token, expected "?" (36:37)
🎁 error
🎁 error   34 |       : never :
🎁 error   35 |   T extends `0b${infer X}` ? Nb.ToNumber<A.Cast<S.Split<X, "">, Nb.Unknown>> :
🎁 error > 36 |   T extends `${infer X extends number}` ? X :
🎁 error      |                                      ^
🎁 error   37 |   `Error: Cannot parse expression '${T}'`
🎁 error   38 |
🎁 error   39 | type Test0 = A.Test<A.AreEqual<E<"5">, 5>>
[...]

cc @Andarist

Tests

probably property-based

"Type instantiation is excessively deep" for `Element` as a predicant

import { p, pa } from "@sthir/predicate"

pa({} as Element, p())
//                ~~~
// Type instantiation is excessively deep and possibly infinite.(2589)

Not sure why it's happening, possibly because Element has many keys... If that's the reason then we need to figure out a test for type with many keys (or union with many constituents) and then don't compute index operator for them (which means now .parentNode, .children, etc won't show up in the intellisense completetion) and only validate it.

make `{ a: string, b: number } & { a: number }` resolve to `never`

This should compile... Or at least find a good reason why it should not :P

import { p, pa } from "@sthir/predicate"

let x = {} as { a: number; b: string } | { a: string; b: number }
if (pa(x, p(".a typeof ===", "number"))) {
  let test: string = x.b    
  //  ~~~~
  // Type 'string | number' is not assignable to type 'string'.
}

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.