Skip to content

Commit 80aae07

Browse files
committed
FR translation: copy folder to translate
1 parent 9f6901c commit 80aae07

File tree

3 files changed

+177
-0
lines changed

3 files changed

+177
-0
lines changed
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
//// { compiler: { }, order: 3 }
2+
3+
// TypeScript's error messages can sometimes be a tad verbose...
4+
// With 3.7, we've taken a few cases which could be particularly
5+
// egregious.
6+
7+
// Nested Properties
8+
9+
let a = { b: { c: { d: { e: "string" } } } };
10+
let b = { b: { c: { d: { e: 12 } } } };
11+
12+
a = b;
13+
14+
// Before, it was 2 lines of code per nested property, which
15+
// quickly meant people learned to read error messages by
16+
// reading the first and then last line of an error message.
17+
18+
// Now they're inline. :tada:
19+
20+
// Previously in 3.6:
21+
//
22+
// Type '{ b: { c: { d: { e: number; }; }; }; }' is not assignable to type '{ b: { c: { d: { e: string; }; }; }; }'.
23+
// Types of property 'b' are incompatible.
24+
// Type '{ c: { d: { e: number; }; }; }' is not assignable to type '{ c: { d: { e: string; }; }; }'.
25+
// Types of property 'c' are incompatible.
26+
// Type '{ d: { e: number; }; }' is not assignable to type '{ d: { e: string; }; }'.
27+
// Types of property 'd' are incompatible.
28+
// Type '{ e: number; }' is not assignable to type '{ e: string; }'.
29+
// Types of property 'e' are incompatible.
30+
// Type 'number' is not assignable to type 'string'
31+
32+
// This can handle working through different types of objects,
33+
// to still give a useful and concise error message.
34+
35+
class ExampleClass {
36+
state = "ok";
37+
}
38+
39+
class OtherClass {
40+
state = 12;
41+
}
42+
43+
let x = { a: { b: { c: { d: { e: { f: ExampleClass } } } } } };
44+
let y = { a: { b: { c: { d: { e: { f: OtherClass } } } } } };
45+
x = y;
46+
47+
// Previously in 3.6:
48+
//
49+
// Type '{ a: { b: { c: { d: { e: { f: typeof OtherClass; }; }; }; }; }; }' is not assignable to type '{ a: { b: { c: { d: { e: { f: typeof ExampleClass; }; }; }; }; }; }'.
50+
// Types of property 'a' are incompatible.
51+
// Type '{ b: { c: { d: { e: { f: typeof OtherClass; }; }; }; }; }' is not assignable to type '{ b: { c: { d: { e: { f: typeof ExampleClass; }; }; }; }; }'.
52+
// Types of property 'b' are incompatible.
53+
// Type '{ c: { d: { e: { f: typeof OtherClass; }; }; }; }' is not assignable to type '{ c: { d: { e: { f: typeof ExampleClass; }; }; }; }'.
54+
// Types of property 'c' are incompatible.
55+
// Type '{ d: { e: { f: typeof OtherClass; }; }; }' is not assignable to type '{ d: { e: { f: typeof ExampleClass; }; }; }'.
56+
// Types of property 'd' are incompatible.
57+
// Type '{ e: { f: typeof OtherClass; }; }' is not assignable to type '{ e: { f: typeof ExampleClass; }; }'.
58+
// Types of property 'e' are incompatible.
59+
// Type '{ f: typeof OtherClass; }' is not assignable to type '{ f: typeof ExampleClass; }'.
60+
// Types of property 'f' are incompatible.
61+
// Type 'typeof OtherClass' is not assignable to type 'typeof ExampleClass'.
62+
// Type 'OtherClass' is not assignable to type 'ExampleClass'.
63+
// Types of property 'state' are incompatible.
64+
// Type 'number' is not assignable to type 'string'
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
//// { compiler: { }, order: 2 }
2+
3+
// The nullish coalescing operator is an alternative to ||
4+
// which returns the right-side expression if the left-side
5+
// is null or undefined.
6+
7+
// In contrast, || uses falsy checks, meaning an empty
8+
// string or the number 0 would be considered false.
9+
10+
// A good example for this feature is dealing with partial
11+
// objects which have defaults when a key isn't passed in.
12+
13+
interface AppConfiguration {
14+
// Default: "(no name)"; empty string IS valid
15+
name: string;
16+
17+
// Default: -1; 0 is valid
18+
items: number;
19+
20+
// Default: true
21+
active: boolean;
22+
}
23+
24+
function updateApp(config: Partial<AppConfiguration>) {
25+
// With null-coalescing operator
26+
config.name = config.name ?? "(no name)";
27+
config.items = config.items ?? -1;
28+
config.active = config.active ?? true;
29+
30+
// Current solution
31+
config.name = typeof config.name === "string" ? config.name : "(no name)";
32+
config.items = typeof config.items === "number" ? config.items : -1;
33+
config.active = typeof config.active === "boolean" ? config.active : true;
34+
35+
// Using || operator which could give bad data
36+
config.name = config.name || "(no name)"; // does not allow for "" input
37+
config.items = config.items || -1; // does not allow for 0 input
38+
config.active = config.active || true; // really bad, always true
39+
}
40+
41+
// You can read more about nullish coalescing in the 3.7 blog post:
42+
//
43+
// https://devblogs.microsoft.com/typescript/announcing-typescript-3-7/
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
//// { compiler: { }, order: 1 }
2+
3+
// Optional chaining reached TC39 Stage 3 consensus during
4+
// 3.7's development. Optional Chaining allows you to write
5+
// code which can immediately stop running expressions when
6+
// it hits a null or undefined.
7+
8+
// Property Access
9+
10+
// Let's imagine we have an album where the artist, and the
11+
// artists bio might not be present in the data. For example
12+
// a compilation may not have a single artist.
13+
14+
type AlbumAPIResponse = {
15+
title: string;
16+
artist?: {
17+
name: string;
18+
bio?: string;
19+
previousAlbums?: string[];
20+
};
21+
};
22+
23+
declare const album: AlbumAPIResponse;
24+
25+
// With optional chaining, you can write
26+
// code like this:
27+
28+
const artistBio = album?.artist?.bio;
29+
30+
// Instead of:
31+
32+
const maybeArtistBio = album.artist && album.artist.bio;
33+
34+
// In this case ?. acts differently than the &&s since &&
35+
// will act differently on "falsy" values (e.g. an empty string,
36+
// 0, NaN, and, well, false).
37+
38+
// Optional chaining will only take null or undefined as
39+
// a signal to stop and return an undefined.
40+
41+
// Optional Element Access
42+
43+
// Property access is via the . operator, the optional chaining
44+
// also works with the [] operators when accessing elements.
45+
46+
const maybeArtistBioElement = album?.["artist"]?.["bio"];
47+
48+
const maybeFirstPreviousAlbum = album?.artist?.previousAlbums?.[0];
49+
50+
// Optional Calls
51+
52+
// When dealing with functions which may or may not exist at
53+
// runtime, optional chaining supports only calling a function
54+
// if it exists. This can replace code where you would traditionally
55+
// write something like: if (func) func()
56+
57+
// For example here's an optional call to the callback from
58+
// an API request:
59+
60+
const callUpdateMetadata = (metadata: any) => Promise.resolve(metadata); // Fake API call
61+
62+
const updateAlbumMetadata = async (metadata: any, callback?: () => void) => {
63+
await callUpdateMetadata(metadata);
64+
65+
callback?.();
66+
};
67+
68+
// You can read more about optional chaining in the 3.7 blog post:
69+
//
70+
// https://devblogs.microsoft.com/typescript/announcing-typescript-3-7/

0 commit comments

Comments
 (0)