Skip to content

Instantly share code, notes, and snippets.

@tkssharma
Created October 28, 2018 01:58
Show Gist options
  • Select an option

  • Save tkssharma/f2299ef11e52681553e2618d6f7d9b1a to your computer and use it in GitHub Desktop.

Select an option

Save tkssharma/f2299ef11e52681553e2618d6f7d9b1a to your computer and use it in GitHub Desktop.
const arr: number[] = [1, 2, 3];
arr.push(4);
const myObj: { x: number } = { x: 1 };
myObj.x = 2;
let n2: string | undefined = Math.random() > 0.5 ? undefined : "test";
// console.log(n2.substring(0, 1)); // Won't compile since can be null
if (n2 !== null) {
console.log(n2.substring(0, 1));
}
function f1(optional?: string): void {
if (optional === undefined) {
// Optional parameter was not provided OR was set to undefined
} else {
// The optional parameter is for sure a string (not undefined)
}
}
let primitiveWithUndefined: number | undefined = undefined;
function functOptionalArg(primitiveOptional?: number): void {
// ...
}
functOptionalArg();
functOptionalArg(undefined);
functOptionalArg(1);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment