-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
148 additions
and
29 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
//// { order: 3 } | ||
|
||
// Deno is a work-in-progress JavaScript and TypeScript | ||
// runtime based on v8 with a focus on security. | ||
|
||
// https://deno.land | ||
|
||
// Deno has a sandbox-based permissions system which reduces the | ||
// access JavaScript has to the file-system or the network and uses | ||
// http based imports which are downloaded and cached locally. | ||
|
||
// Here is an example of using deno for scripting: | ||
|
||
import compose from "https://deno.land/x/denofun/lib/compose.ts"; | ||
|
||
function greet(name: string) { | ||
return `Hello, ${name}!`; | ||
} | ||
|
||
function makeLoud(x: string) { | ||
return x.toUpperCase(); | ||
} | ||
|
||
const greetLoudly = compose(makeLoud, greet); | ||
|
||
// Echos "HELLO, WORLD!." | ||
greetLoudly("world"); | ||
|
||
|
||
import concat from "https://deno.land/x/denofun/lib/concat.ts"; | ||
// Returns "helloworld" | ||
concat("hello", "world"); | ||
|
This file was deleted.
Oops, something went wrong.
Empty file.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Node is a very popular JavaScript runtime built on v8, | ||
// the JavaScript engine which powers Chrome. You can use it | ||
// to build servers, front-end clients and anything in-between. | ||
|
||
// https://nodejs.org/en/ | ||
|
||
// Node comes with a set of core libraries which extend the | ||
// JavaScript runtime, they range from path handling: | ||
|
||
import {join} from "path" | ||
const myPath = join("~", "downloads", "todo_list.json") | ||
|
||
// To file manipulation: | ||
|
||
import {readFileSync} from "fs" | ||
const todoListText = readFileSync(myPath, "utf8") | ||
|
||
interface TODO { | ||
title: string | ||
description: string | ||
done: boolean | ||
} | ||
|
||
const todoList = JSON.parse(todoListText) as TODO[] | ||
|
||
// And process handling: | ||
import {spawnSync} from "child_process" | ||
todoList.filter(todo => !todo.done) | ||
.forEach(todo => { | ||
// Use the ghi client to create an issue for every todo | ||
// list item which hasn't been completed yet | ||
spawnSync(`ghi open --message "${todo.title}\n${todo.description}" `) | ||
}); | ||
|
||
// TypeScript has up-to-date type definitions for all of the | ||
// built in modules via DefinitelyTyped - which means you | ||
// can write node programs with strong type coverage |
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
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
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
File renamed without changes.
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
File renamed without changes.
File renamed without changes.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
// It might be easiest to start of the discussion of | ||
// widening and narrowing with an example: | ||
|
||
const welcomeString = "Hello There" | ||
let replyString = "Hey" | ||
|
||
// Aside from the text differences of the strings, welcomeString | ||
// is a const (which means the value will never change) | ||
// and replyString is a let (which means it can change) | ||
|
||
// If you hover over both variables, you get very different | ||
// type information from TypeScript: | ||
// | ||
// const welcomeString: "Hello There" | ||
// | ||
// let replyString: string | ||
// | ||
|
||
// TypeScript has inferred the type of welcomeString to be | ||
// the literal string "Hello There" whereas replyString | ||
// is general string. | ||
|
||
// This is because a let needs to have a wider type, you | ||
// could set replyString to be any other string - which means | ||
// it has a wider set of possibilities. | ||
|
||
replyString = "Hi :wave:" | ||
|
||
// If replyString had the string literal type "Hey" - then | ||
// you could never change the value because it could only | ||
// change to "Hey" again. | ||
|
||
// Widening and Narrowing types is about expanding and reducing | ||
// the possibilities which a type could represent. | ||
|
||
// An example of type narrowing is working with unions, the | ||
// example on code flow analysis is almost entirely based on | ||
// narrowing: example:code-flow | ||
|
||
// Type narrowing is what powers the strict mode of TypeScript | ||
// via the nullability checks. With strict mode turned off, | ||
// markers for nullability like undefined and null are ignored | ||
// in a union. | ||
|
||
declare const quantumString: string | undefined | ||
// This will fail in strict mode only | ||
quantumString.length | ||
|
||
// In strict mode the onus is on the code author to ensure | ||
// that the type has been narrowed to the non-null type. | ||
// Usually this is as simple as an if check | ||
|
||
if (quantumString) { | ||
quantumString.length | ||
} | ||
|
||
// In strict mode the type quantumString has two representations. | ||
// Inside the if, the type was narrowed to just string. | ||
|
||
// https://mariusschulz.com/blog/literal-type-widening-in-typescript | ||
// https://sandersn.github.io/manual/Widening-and-Narrowing-in-Typescript.html |
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