Skip to content

Commit

Permalink
2023-09-13T09:17:13.105Z
Browse files Browse the repository at this point in the history
  • Loading branch information
mattpocock committed Sep 13, 2023
1 parent c62786b commit 8f91ed7
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
type Expect<T extends true> = T;
type Equal<X, Y> = (<T>() => T extends X ? 1 : 2) extends <T>() => T extends Y
? 1
: 2
? true
: false;

/**
* Checks that Y is assignable to X.
*
* For instance, `Extends<string, 'a'>` is true. This is because
* 'a' can be passed to a function which expects a string.
*
* But `Extends<'a', string>` is false. This is because a string
* CANNOT be passed to a function which expects 'a'.
*/
type Extends<X, Y> = Y extends X ? true : false;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "exercise",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"dev": "tsc --watch"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const array = [1, 2, 3];

const mightNotExist = array[3];

type test = Expect<Equal<typeof mightNotExist, number | undefined>>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"compilerOptions": {
"target": "ES2022",
"module": "ESNext",
"moduleResolution": "Bundler",
"esModuleInterop": true,
"noEmit": true,
"strict": true,
"skipLibCheck": true,
"isolatedModules": true,
},
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
type Expect<T extends true> = T;
type Equal<X, Y> = (<T>() => T extends X ? 1 : 2) extends <T>() => T extends Y
? 1
: 2
? true
: false;

/**
* Checks that Y is assignable to X.
*
* For instance, `Extends<string, 'a'>` is true. This is because
* 'a' can be passed to a function which expects a string.
*
* But `Extends<'a', string>` is false. This is because a string
* CANNOT be passed to a function which expects 'a'.
*/
type Extends<X, Y> = Y extends X ? true : false;
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"name": "exercise",
"version": "1.0.0",
"main": "index.js",
"scripts": {
"dev": "tsc --watch"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const array = [1, 2, 3];

const mightNotExist = array[3];

type test = Expect<Equal<typeof mightNotExist, number | undefined>>;
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"compilerOptions": {
"target": "ES2022",
"module": "ESNext",
"moduleResolution": "Bundler",
"esModuleInterop": true,
"noEmit": true,
"strict": true,
"skipLibCheck": true,
"isolatedModules": true,
"noUncheckedIndexedAccess": true
},
}
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
// https://github.com/tsconfig/bases

// Note that strictest is not what I recommend

0 comments on commit 8f91ed7

Please sign in to comment.