Skip to content

Adds updated examples #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 44 commits into from
Sep 9, 2019
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
927c7bd
Adds the first set of examples for the playground
orta Aug 12, 2019
d44bd37
Reword/repunctuate a few sentences
sandersn Aug 12, 2019
95f9c95
Iteration two, with a 60 char line limit
orta Aug 13, 2019
0434f68
Adds a JSDoc example
orta Aug 13, 2019
2bd6028
Update Code Flow.ts
DanielRosenwasser Aug 14, 2019
a553201
Merge pull request #45 from microsoft/code-flow-suggestions
Aug 14, 2019
d602c09
More example work
orta Aug 14, 2019
1afb806
Merge branch 'examples' of https://github.com/microsoft/TypeScript-we…
orta Aug 14, 2019
6059759
Add some notes
orta Aug 15, 2019
074de8c
More examples
orta Aug 15, 2019
a642547
More examples
orta Aug 15, 2019
dddf138
Adds react + starts the DOM
orta Aug 16, 2019
2987246
More work on the examples
orta Aug 16, 2019
c999b48
More examples
orta Aug 16, 2019
bca8398
More examples
orta Aug 16, 2019
93739d6
Adds async await
orta Aug 17, 2019
e3a1ac6
Classes 101
orta Aug 17, 2019
472b9c4
Errors WI
orta Aug 18, 2019
76fdb9d
Merge
orta Aug 18, 2019
779b772
Meta types
orta Aug 18, 2019
8f56cbf
Adds enums + never + unknown
orta Aug 18, 2019
f0ddef6
More examples work
orta Aug 18, 2019
bcb31b9
Script generating a TOC of the examples for the playground to work from
orta Aug 18, 2019
a617170
Classes
orta Aug 18, 2019
d9a2c3e
Adds this doc
orta Aug 18, 2019
c0b385f
Nominal types
orta Aug 18, 2019
c50b141
More work on examples
orta Aug 19, 2019
a1ca06f
More examples
orta Aug 26, 2019
986d1ea
More example work
orta Aug 28, 2019
fad2001
More examples with default TS formatting
orta Aug 29, 2019
d3ef1a6
Update to master
orta Aug 30, 2019
1d84e6d
Adds more examples
orta Aug 31, 2019
e9aae91
Add gulp commands for migrating examples into the site
orta Aug 31, 2019
134b157
Handles the website side of the infra for TS playground examples
orta Sep 1, 2019
c30a12c
Soundness note
orta Sep 2, 2019
ad93ee2
Update playground
orta Sep 2, 2019
6281df9
Adds a note about mixing union + intersections for required but choos…
orta Sep 7, 2019
80c7e2f
Adds a note about mixing union + intersections for required but choos…
orta Sep 7, 2019
b9dcb9c
Try let checkout handle submodules
orta Sep 7, 2019
aca65f5
Remove the extra submodule steps
orta Sep 7, 2019
ade6eb1
Fix the playground submodule
orta Sep 7, 2019
931eafa
Final revision
orta Sep 9, 2019
fc7f8e6
Revise the docs root, and the connect pages
orta Sep 9, 2019
902bf35
Update playground
orta Sep 9, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions Examples/en/JavaScript/External APIs/TypeScript + Deno.ts
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");

37 changes: 37 additions & 0 deletions Examples/en/JavaScript/External APIs/TypeScript + Node.ts
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"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

CommonJS modules should be imported with import = require( until ES6/CJS interop gets worked out

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've never seen imports written with that format in production code with node, I know the reason the syntax exists - but is it really worth having to explain the different syntax here for this only for it to be thrown away by everyone?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Destructuring import of CommonJS works today and will presumably work after the compat story is in place. I think it should be safe. People do need to learn the TS-specific syntax sometime but I’m not sure it’s important in a sample.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've never seen imports written with that format in production code with node, I know the reason the syntax exists - but is it really worth having to explain the different syntax here for this only for it to be thrown away by everyone?

It's not getting "thrown away", probably ever. People thought that three years ago, and they're still wrong about that now~

Destructuring import of CommonJS works today and will presumably work after the compat story is in place.

As of right now, that's not the case. Core modules like path have an esm facade that provides named imports, but in general, no. It's one of the things I and others would still like to see changed.

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
113 changes: 113 additions & 0 deletions Examples/en/JavaScript/External APIs/TypeScript + React.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
//// { order: 2, compiler: { jsx: "react" } }

// React is a popular library for creating user interfaces.
// It provides a JavaScript abstraction for creating view
// components using a JavaScript language extension called
// JSX.

// TypeScript supports JSX, and provides a rich set of
// type tools to richly model how components connect.

// To get started with understanding how React comes
// together. First we'll look at how generic interfaces
// works in TypeScript. We're going to create an faux-React
// functional component.

interface FuaxactFunctionComponent<Props extends {}> {
(props: Props, context?: any): FuaxactFunctionComponent<any> | null
}

// This creates a function which is generic with a Props
// variable which has to be an object. The component function
// returns either another component function or null.

// The other component API is a class-based one, here's a
// simplified version of that API

interface FuaxactClassComponent<Props extends {}, State = {}> {
props: Props
state: State

setState: (prevState: State, props: Props) => Props
callback?: () => void
render() : FuaxactClassComponent<any> | null

}

// Because this class can have both Props and State - it has
// two generic arguments which are used throughout the class.

// The React library comes with it's own type definitions
// like these but are much more comprehensive. Let's bring
// those into our playground and explore a few components.

import React from 'react';

// Your props are your public API, so it's worth taking the
// time to use JSDoc to explain how it works:

export interface Props {
/** The user's name */
name: string;
/** Should the name be rendered in bold */
priority?: boolean
}

const PrintName: React.FC<Props> = (props) => {
return (
<div>
<p style={{ fontWeight: props.priority ? "bold": "normal"}}>OK</p>
</div>
)
}

// You can play with the new component's usage below:

const ShowUser: React.FC<Props> = (props) => {
return <PrintName name="Ned" />
}

// TypeScript supports providing intellisense inside
// the {} in an attribute

let username = "Cersei"
const ShowStoredUser: React.FC<Props> = (props) => {
return <PrintName name={username} priority />
}

// TypeScript works with modern React code too, here you can
// see that count and setCount have correctly been inferred
// to use numbers based on the initial value passed into
// useState.

import { useState, useEffect } from 'react';

const CounterExample = () => {
const [count, setCount] = useState(0);

useEffect(() => {
document.title = `You clicked ${count} times`;
});

return (
<div>
<p>You clicked {count} times</p>
<button onClick={() => setCount(count + 1)}>
Click me
</button>
</div>
);
}

// React and TypeScript is a really, really big topic
// but the fundamentals are pretty small: TypeScript
// supports JSX, and the rest is handled by the React
// typings from definitely typed.

// You can learn more about using React with TypeScript
// from these sites:

// https://github.com/typescript-cheatsheets/react-typescript-cheatsheet
// https://egghead.io/courses/use-typescript-to-develop-react-applications
// https://levelup.gitconnected.com/ultimate-react-component-patterns-with-typescript-2-8-82990c516935

77 changes: 77 additions & 0 deletions Examples/en/JavaScript/External APIs/TypeScript + Web.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
//// { order: 0 }

// The DOM (Document Object Model) is the underlying API for
// working with a webpage, and TypeScript has great support
// for that API.

// Let's create a popover to show when you press run in
// the toolbar above.

const popover = document.createElement("div")
popover.id = "example-popover"

// Note that popover is correctly typed to be a HTMLDivElement
// specifically because we passed in "div".

// To make it possible to re-run this code, we'll first
// add a function to remove the popover it it was already there.

const removePopover = () => {
const existingPopover = document.getElementById(popover.id)
if (existingPopover && existingPopover.parentElement) {
existingPopover.parentElement.removeChild(existingPopover)
}
}

// Then call it right away

removePopover()

// We can set the inline styles on the element via the
// .style property on a HTMLElement - this is fully typed

popover.style.backgroundColor = "#0078D4"
popover.style.color = "white"
popover.style.border = "1px solid black"
popover.style.position = "fixed"
popover.style.bottom = "10px"
popover.style.right = "20px"
popover.style.width = "200px"
popover.style.height = "100px"
popover.style.padding = "10px"

// Including more obscure, or deprecated CSS attributes
popover.style.webkitBorderRadius = "4px"

// To add content to the popover, we'll need to add
// a paragraph element and use it to add some text

const message = document.createElement("p")
message.textContent = "Here is an example popover"

// And we'll also add a close button

const closebutton = document.createElement("a")
closebutton.textContent = "X"
closebutton.style.position = "absolute"
closebutton.style.top = "3px"
closebutton.style.right = "8px"
closebutton.style.color = "white"

closebutton.onclick = () => { removePopover() }

// Then add all of these elements on to the page
popover.appendChild(message)
popover.appendChild(closebutton)
document.body.appendChild(popover)

// If you hit "Run" above, then a popup should appear
// in the bottom left, which you can close by clicking
// on the x in the top right of the popup.

// This example shows how you can work with the DOM API
// in TypeScript - but it only shows a subset of working
// with the browser's APIs. TypeScript supports

// There is an extended example for TypeScript with the
// WebGL example available here: example:typescript---webgl
Loading