Skip to content
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

Update Playground Examples #25

Closed
RyanCavanaugh opened this issue May 16, 2018 · 11 comments
Closed

Update Playground Examples #25

RyanCavanaugh opened this issue May 16, 2018 · 11 comments

Comments

@RyanCavanaugh
Copy link
Member

The TypeScript Playground examples are... not great. We deserve a new set.

General principles

  • Don't lean on classes
  • Show off type inference where possible - minimal annotations
  • Emphasize working with JavaScript
  • Show both error-finding and good intellisense

Please Suggest More!


Basic Types & Inference

const array = ["red", "green", "blue", "yellow"];
array.forEach(color => {
  // TypeScript knows 'color' is a string and warns on the incorrect method name
  console.log(color.toUppercase());

  // Try typing 'color.' on this line to see members of string
  color
});

// TypeScript knows s has one of two string values.
// This can detect some different errors
const s = Math.random() > 0.5 ? "aaa" : "bbb";
// ... like the spelling error here
if (s === "aa") {
 // oops
}
// ... and the logic error here!
if (s === "aaa" && s === "bbb") {
  // oops
}

Functions

function check(s: string) {
  return s.length > 3;
}

// TypeScript warns if we get the argument count wrong
check();
// Or if the argument type is wrong
check(10);

// The return type of the function is inferred,
// so errors like this are automatically detected
if (check("hi") === "world") {
  // Never true because 'world' isn't a boolean
}

Union Types

// TypeScript can work with objects of mixed types
const items = ["red", 10, 15, "blue"];
for (const i of items) {
  if (typeof i === "string") {
    // TypeScript knows 'i' is 'string' here
    console.log(i.toUpperCase());
  } else {
    // 'i' is of type 'number' here
    console.log(i * 2);
  }
}

Generics

ES6 Classes

What else?

@weswigham
Copy link
Member

Also TSX.

@MartinJohns
Copy link
Contributor

MartinJohns commented May 16, 2018

Add an example showing where type annotations do matter: When declaring variables of object literals.

interface Test {
    readonly a: number;
    readonly b: number;
}

const a1 = { a: 1, b: 2 };
const a2: Test = { a: 1, b: 2 };

When deleting a property from Test the first variable will still work fine, but the second will cause an error (#3755). Same would apply when returning an object literal from a function. While technically it is working and the object literal still fulfills the interface with the extra properties, it's still better to notice the developer so he can remove code that is not necessary anymore.


Add an example demonstrating why casts are often a bad idea and undermine type safety:

interface Test {
    readonly a: number;
    readonly b: number;
}

const a = <Test>{ a: 5 };
a.b; // Type is `number`, but value is `undefined`.

@donaldpipowitch
Copy link

One about proper usage of built-in helpers like ReturnType.

@orta
Copy link
Contributor

orta commented May 17, 2018

It's a little bit verbose, but I think the example React component in TypeScript-React-Native-Starter covers a lot of the value in using TypeScript with React

@styfle
Copy link
Contributor

styfle commented May 17, 2018

I suggest looking this playground from @agentcooper which has some additional examples but also has even more features than the official playground.

Also, I'll link this related issue: agentcooper/typescript-play#4

@weswigham
Copy link
Member

  • An example using literal types and discriminated unions.
  • An example for import types.
  • An example for mapped types.
  • An example for conditional types.

@mhegazy
Copy link

mhegazy commented May 18, 2018

‘ts-check’ example as well.

@orta orta transferred this issue from microsoft/TypeScript Jul 10, 2019
@orta
Copy link
Contributor

orta commented Jul 11, 2019

Blast from the past on this issue - but I've moved it to the TS website now, I've tried to think of the examples from quite far back - helping folks look for simpler JS use cases (if you're new) all the way to advanced TS things like conditional types. What do folks think of this general progression?

Screen Shot 2019-07-11 at 6 49 46 PM

( To do it I'd have to add JS support, and type acquisition to the playground - but that's achievable )

@orta
Copy link
Contributor

orta commented Jul 12, 2019

There isn't done for showing how to work with the DOM! Added

@orta
Copy link
Contributor

orta commented Jul 13, 2019

OK... well, on 2nd round when trying to think about the advanced types much harder. I ended up coming up with "a lot" but also TS things which weren't quite "advanced" - so:

Screen Shot 2019-07-15 at 2 29 55 PM

then for things which are really typescript and not just day-to-day JS

Screen Shot 2019-07-15 at 2 30 11 PM

... ideally there'd be one more. Maybe that could be example apps or something which when you run it would be cool, sorta like the current retracer

@RyanCavanaugh RyanCavanaugh mentioned this issue Aug 12, 2019
53 tasks
@orta
Copy link
Contributor

orta commented Sep 23, 2019

This is shipped!

@orta orta closed this as completed Sep 23, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants