Giter Club home page Giter Club logo

Comments (4)

ChristianMurphy avatar ChristianMurphy commented on May 28, 2024

@chmac predicate is a narrower type than boolean, which is why TS is erroring in this case.
https://www.typescriptlang.org/docs/handbook/advanced-types.html#using-type-predicates


Using a TS Predicate ensures that when used with a conditional, inside the if scope, TypeScript knows that the variable passed, is the type defined by the generic in is.
For example:

const isHeading = (node: unknown): node is Heading =>
typeof node === 'object' && node !== null && (node as Node).type === 'heading'

if (is<Heading>(heading, 'heading')) {
const maybeHeading: Heading = heading
// $ExpectError
const maybeNotHeading: Element = heading
}

coming into this function, TypeScript only knows heading is some variable, within the if scope, TypeScript knows that is must be type Node and more specifically a Heading node and allows safe access to all properties provided by that type.
Using a boolean would only safely allow narrowing the type to Node, because we'd have no way to determine what more specific type is wanted.
Allowing a fully generic type without a predicate would allow for a runtime exception such as the one in the example here: #16 (comment)


In your example try:

import { Node, Parent } from "unist";
import visit from "unist-util-visit";

export const insertBefore = (
  tree: Parent,
  predicate: (node: Node, index?: number, parent?: Parent) => node is Node,
  transform: (node: Node) => Node
) => {
  visit(tree, predicate, (node, index, parent) => {});
};

or even better

import { Node, Parent } from "unist";
import { Test } from "unist-util-is";
import visit from "unist-util-visit";

export const insertBefore = <T extends Node>(
  tree: Parent,
  predicate: Test<T>,
  transform: (node: T) => Node
) => {
  visit(tree, predicate, (node, index, parent) => {});
};

Also reference the type tests in https://github.com/syntax-tree/unist-util-visit/blob/master/types/unist-util-visit-tests.ts for more examples.

from unist-util-is.

chmac avatar chmac commented on May 28, 2024

@ChristianMurphy I don't think I've 100% grok'd this yet, but will spend some more time on it. Thanks for the detailed explanation.

It seems like you made a reasoned decision which I misunderstood. What about adding something to the docs sharing how to use this package in TypeScript? I'd be happy to have a go at submitting a PR to that effect if that would be helpful.

from unist-util-is.

ChristianMurphy avatar ChristianMurphy commented on May 28, 2024

More documentation is welcome.
Rather than making it repository specific, it could be good to add it to a guide about using Unified with TypeScript. See unifiedjs/unifiedjs.github.io#7 for more information on guides.

Types like Test, Plugin, and Transformer are used in many packages, having a central guide could be helpful to point folks in the right direction.

from unist-util-is.

wooorm avatar wooorm commented on May 28, 2024

If I understand this conversation correctly, I believe I can close this.
If that is a mistake, please comment here and we can open this again.

from unist-util-is.

Related Issues (6)

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.