This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const childIsA5 = isAorBorCorDorEorF && root.child.kind === 'A'; | |
// ^^^^^ - Error |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface A { | |
kind: 'A'; | |
child: Node; | |
} | |
interface B { | |
kind: 'B'; | |
child: Node; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function setupComponent(component: Component) { | |
let isUsingTextArea = 'rows' in component.inputArea.control; | |
if (isUsingTextArea) { // Narrowing does not occur | |
component.inputArea.control.rows = 25; // Compiler error | |
// ... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface InputArea { | |
control: HTMLTextAreaElement | HTMLInputElement; | |
} | |
interface Component { | |
inputArea: InputArea; | |
} | |
function setupComponent(component: Component) { | |
if ('rows' in component.inputArea.control) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
//... | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const isTextArea = 'rows' in input; | |
if (isTextArea) { | |
// Narrowing has NOT taken place | |
input.rows = 25; // Compiler error | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
NewerOlder