Comments (9)
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.
For a moment I thought this could be a solution for dsherret/ts-morph#357, and I got so excited
from conditional-type-checks.
@lazarljubenovic hah, I wish! Yeah, that's still not exposed in the compiler api 😢
from conditional-type-checks.
and IsNotAssignable
type
from conditional-type-checks.
@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.
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.
@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.
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.
https://github.com/unional/type-plus/blob/main/ts/predicates/CanAssign.ts
from conditional-type-checks.
Related Issues (17)
- Add AssertTrue/AssertFalse HOT 3
- IsAny improvement HOT 2
- Add a function function eq<A, B>(x: IsExact<A, B>) {} HOT 1
- IsExact of two identical enums is not working. HOT 2
- Add constraints to type parameters of `Has` HOT 2
- Unclear how to check for deep equality of nested types HOT 4
- IsExact fails with string enum item and string HOT 1
- Fails with recursive types HOT 3
- IsExactType does not fail for nullable equalling non-nullable HOT 1
- IsExact does not handle `readonly` modifier HOT 1
- docs(): Failure Assertion
- IsAny returns 'boolean' HOT 4
- how do I use this library with jest testing? HOT 10
- Add `HasProperty` HOT 2
- IsExact - Object type where one property type is the `any` type leads to result being true
- IsExact - Optional properties not handled
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from conditional-type-checks.