Skip to content

Commit 9318d55

Browse files
committed
New way of defining types
See: microsoft/TypeScript#364 (comment)
1 parent 616895d commit 9318d55

File tree

1 file changed

+22
-21
lines changed

1 file changed

+22
-21
lines changed

README.md

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,31 +11,31 @@ TypeScript could benefit from a similar units of measure feature and expand to i
1111

1212
## Defining Type Annotations
1313

14-
Type annotations are defined similarly to ambient types:
14+
Type annotations are defined as such:
1515

1616
```typescript
17-
declare type <unit-name> [ = measure ];
17+
type <annotation-name> [extends <type-name>] [ = measure ];
1818
```
1919

20-
The optional measure part can be used to define a new type in terms of previously defined types. Note that this composite type cannot be used with non-`number` types.
20+
The optional measure part can be used to define a new types in terms of previously defined types. Note that this composite type cannot be used with non-`number` types.
2121

2222
**Examples:**
2323

2424
```typescript
25-
declare type m;
26-
declare type s;
27-
declare type a = m/s^2;
28-
declare type email;
25+
type m extends number;
26+
type s extends number;
27+
type a = m/s^2;
28+
type email extends string;
2929
```
3030

3131
Note: The caret symbol does not denote a bitwise XOR operator, but rather an exponent. In this case, `m/s^2` is equivalent to `m/s/s`.
3232

3333
## Use with Number
3434

3535
```typescript
36-
declare type m;
37-
declare type s;
38-
declare type a = m/s^2;
36+
type m extends number;
37+
type s extends number;
38+
type a = m/s^2;
3939

4040
var acceleration = 12<a>,
4141
time = 10<s>;
@@ -55,7 +55,7 @@ acceleration += 12<m/s^2> * 10<s>; // compile error -- Cannot convert number<m/s
5555
## Use with String, Boolean, and Date
5656

5757
```typescript
58-
declare type email;
58+
type email extends string;
5959

6060
function sendEmail(email: string<email>, message : string) {
6161
// send the email in here
@@ -66,23 +66,23 @@ sendEmail(myEmail, "Hello!"); // valid
6666
sendEmail("some string", "Hello!"); // compile error -- Cannot convert string to string<email>
6767
```
6868

69-
The following is invalid with non-number types:
69+
The following is invalid:
7070

7171
```typescript
72-
declare type m;
73-
declare type s;
74-
declare type a = m/s^2;
72+
type m extends number;
73+
type s extends number;
74+
type a = m/s^2;
7575

76-
var myString : string<a>; // compile error -- Cannot use composite types with non-number types
76+
var myString : string<a>; // compile error -- Cannot use number types with a string
7777
```
7878

7979
## Additional Cases
8080

8181
```typescript
82-
declare type m;
83-
declare type email;
84-
declare type startDate;
85-
declare type flag;
82+
type m extends number;
83+
type email extends string;
84+
type startDate extends Date;
85+
type flag extends boolean;
8686

8787
var num = new Number<m>(),
8888
str = new String<email>(),
@@ -92,9 +92,10 @@ var num = new Number<m>(),
9292

9393
## Additional Considerations
9494

95-
It might be beneficial to have a type constraint on these defined types. For example:
95+
The defintion statement could also be like one of the following (or a combination of):
9696

9797
```typescript
98+
type string<email>;
9899
declare type email : string;
99100
```
100101

0 commit comments

Comments
 (0)