Coder Social home page Coder Social logo

Add IsAssignable about conditional-type-checks HOT 9 OPEN

dsherret avatar dsherret commented on May 4, 2024 3
Add IsAssignable

from conditional-type-checks.

Comments (9)

Gerrit0 avatar Gerrit0 commented on May 4, 2024 1

type IsAssignable<T, U> = T extends U ? true : false

This isn't a good implementation. With IsAssignable<A | B, B> it gives boolean because T is a bare type parameter and we distribute. A better implementation would be:

type IsAssignable<T, U> = [T] extends [U] ? true : false

from conditional-type-checks.

lazarljubenovic avatar lazarljubenovic commented on May 4, 2024

For a moment I thought this could be a solution for dsherret/ts-morph#357, and I got so excited 😄 But just to be sure: although all of assertions in this repo are kinda meta-ish, none would be of any help when analyzing code statically, right? You'd still need to actually run the full TypeScript compiler and enable type checking in order to statically determine if two types are assignable to one other, by only looking at source code?

from conditional-type-checks.

dsherret avatar dsherret commented on May 4, 2024

@lazarljubenovic hah, I wish! Yeah, that's still not exposed in the compiler api 😢

from conditional-type-checks.

smmoosavi avatar smmoosavi commented on May 4, 2024

and IsNotAssignable type

from conditional-type-checks.

dsherret avatar dsherret commented on May 4, 2024

@smmoosavi wouldn't need that as the API currently supports checking that one of these types resolves to false. For example:

assert<IsNullable<string>>(false);
// or
type doTest = AssertFalse<IsNever<typeof result>>;
// or
type doTest2 = Assert<Has<typeof result, number>, false>;

from conditional-type-checks.

smmoosavi avatar smmoosavi commented on May 4, 2024

It is about readability.

type doTest = AssertTrue<
  IsNotAssignable<
    ActionListener<Animal>,
    ActionListener<Dog>
    >,
  >

or

type doTest = AssertTrue<
  IsAssignable<
    ActionListener<Dog>,
    ActionListener<Animal>
    >,
  >

from conditional-type-checks.

dsherret avatar dsherret commented on May 4, 2024

@smmoosavi seems just as readable?

type doTest = AssertFalse<
  IsAssignable<
    ActionListener<Animal>,
    ActionListener<Dog>
  >
>

Should there be IsNotX types for everything to be consistent? Why not just?

type doTest = AssertTrue<
  Not<IsAssignable<
    ActionListener<Animal>,
    ActionListener<Dog>
  >>
>

...though, I don't think that is worth adding.

from conditional-type-checks.

smmoosavi avatar smmoosavi commented on May 4, 2024

potential implementation

true if T is assignable to U

type IsAssignable<T, U> = T extends U ? true : false

example:

type IsAssignable<T, U> = T extends U ? true : false

type C<T> = (c: T) => void 


interface A{ a: string }
interface B{ b: number }
interface AB{a:string,b:number}

type CA = C<A>
type CB = C<B>
type CAB = C<AB>

type x1 = IsAssignable<AB, A> // true 
type x2 = IsAssignable<B, A> // false
type x3 = IsAssignable<A, AB> // false
type x4 = IsAssignable<CA, CAB> // true
type x5 = IsAssignable<CAB, CA> // false

T extends U ? X : Y

The type above means when T is assignable to U the type is X, otherwise the type is Y.

source: https://www.typescriptlang.org/docs/handbook/advanced-types.html#conditional-types

from conditional-type-checks.

unional avatar unional commented on May 4, 2024

https://github.com/unional/type-plus/blob/main/ts/predicates/CanAssign.ts

🌷

from conditional-type-checks.

Related Issues (17)

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.