Skip to content

Commit c19c274

Browse files
authored
Update literal-types.md
1 parent 7fe8e2f commit c19c274

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

docs/types/literal-types.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,19 @@ const test = {
6262
iTakeFoo(test.someProp); // Okay!
6363
```
6464

65+
or use a type annotation that helps TypeScript infer the correct thing at the point of declaration:
66+
67+
```
68+
function iTakeFoo(foo: 'foo') { }
69+
type Test = {
70+
someProp: 'foo',
71+
}
72+
const test: Test = { // Annotate - inferred someProp is always === 'foo'
73+
someProp: 'foo'
74+
};
75+
iTakeFoo(test.someProp); // Okay!
76+
```
77+
6578
### Use cases
6679
Valid use cases for string literal types are:
6780

0 commit comments

Comments
 (0)