Skip to content

Instantly share code, notes, and snippets.

@eamonnboyle
eamonnboyle / TransitiveDepthLimit.ts
Created August 27, 2021 10:22
Nested Aliased Conditional Fail Check
const childIsA5 = isAorBorCorDorEorF && root.child.kind === 'A';
// ^^^^^ - Error
@eamonnboyle
eamonnboyle / TransitiveDepthLimit.ts
Created August 27, 2021 10:21
Nested Aliased Condition Successful Checks
const childIsA1 = isAorB && root.child.kind === 'A';
const childIsA2 = isAorBorC && root.child.kind === 'A';
const childIsA3 = isAorBorCorD && root.child.kind === 'A';
const childIsA4 = isAorBorCorDorE && root.child.kind === 'A';
@eamonnboyle
eamonnboyle / TransitiveDepthLimit.ts
Created August 27, 2021 10:20
Nested Aliased Conditions
const isA = root.kind === 'A';
const isAorB = isA || root.kind === "B";
const isAorBorC = isAorB || root.kind === "C";
const isAorBorCorD = isAorBorC || root.kind === "D";
const isAorBorCorDorE = isAorBorCorD || root.kind === "E";
const isAorBorCorDorEorF = isAorBorCorDorE || root.kind === "F";
@eamonnboyle
eamonnboyle / TransitiveDepthLimit.ts
Created August 27, 2021 10:19
Large Discriminated Union
interface A {
kind: 'A';
child: Node;
}
interface B {
kind: 'B';
child: Node;
}
@eamonnboyle
eamonnboyle / AliasedCondition.ts
Created August 27, 2021 10:18
Nested Property Aliased Condition Fail
function setupComponent(component: Component) {
let isUsingTextArea = 'rows' in component.inputArea.control;
if (isUsingTextArea) { // Narrowing does not occur
component.inputArea.control.rows = 25; // Compiler error
// ...
@eamonnboyle
eamonnboyle / NestedPropertyGuard.ts
Created August 27, 2021 10:17
Nested Property Guards
interface InputArea {
control: HTMLTextAreaElement | HTMLInputElement;
}
interface Component {
inputArea: InputArea;
}
function setupComponent(component: Component) {
if ('rows' in component.inputArea.control) {
@eamonnboyle
eamonnboyle / AliasedCondition.ts
Created August 27, 2021 10:16
Only conditions are Aliased
let primitiveType = typeof primitive;
switch (primitiveType) {
case "number": return primitive.toFixed(2); // No narrowing - compiler error
case "string": return primitive.toUpperCase(); // No narrowing - compiler error
//...
}
@eamonnboyle
eamonnboyle / AliasedCondition.ts
Created August 27, 2021 10:14
More Complex Aliased Conditions
const isDog = animal.kind === 'Dog';
const isCat = animal.kind === 'Cat';
const canSpeak = isDog || isCat; // Also retains CFA information
if (!canSpeak) {
throw new Error('What does the Fox say?');
}
return isCat
? animal.purr()
@eamonnboyle
eamonnboyle / AliasedCondition.ts
Created August 27, 2021 10:13
TS 4.4 - Aliased Condition
const isTextArea = 'rows' in input;
if (isTextArea) {
// Narrowing has NOT taken place
input.rows = 25; // Compiler error
}
@eamonnboyle
eamonnboyle / AssertionFunctions.ts
Created August 27, 2021 10:09
Assertion Functions
function assertIsTextArea(input: HTMLInputElement | HTMLTextAreaElement): asserts input is HTMLTextAreaElement {
if (!('rows' in input)) {
throw new Error("Expected a HTMLTextAreaElement");
}
}
assertIsTextArea(input);
// If we get to this line, no exception has been thrown
// Therefore, input has type HTMLTextAreaElement