Repository navigation
Creating a separate interface for IncomingMessageHeaders in node/index.d.ts - #17157
2 commits merged into
Conversation
This allows for IncomingMessage.headers types to be extensible per project. Suppose a node service expects a fixed set of headers of certain type (say string), that service can specify custom IncomingMessageHeaders with more concrete types defined.
For example
```
interface IncomingMessageHeaders {
"host": string;
"set-cookie": string[]
}
```
|
I like the idea but I tried a slightly different variant to extend the interface in my application and it fails. As headers are optional I tried the following: but this results in I use typescript 2.3 and have set strict to true in my tsconfig.json. If I explicitly add |
|
I think |
This definition adds undefined in addition to string and string[].
|
@Andy-MS Any idea why build fails? Not able to make sense out of the logs. |
|
Looks like that's broken in master. I'll fix separately. Thanks! (Ref: #17141 (comment)) |
|
Could you provide example when |
|
Found the problem source c9e01b3#commitcomment-22503480 |
prints the following: ==> At least the |
|
Thanks. I see problem now. We can't declare interface IncomingMessageHeaders {
[key: string]: string;
"set-cookie": string[];
}because |
|
Now I should use type assertion in each header access <string>req.headers['foo']This is very annoying. Instead we can declare interface interface CommonHeaders {
[key: string]: string;
}
interface SpecialHeaders {
"set-cookie": string[];
}
type IncomingMessageHeaders = CommonHeaders & SpecialHeaders;Then we can remove type assertions: function foo(headers: IncomingMessageHeaders) {
headers['a'].trim();
headers['set-cookie'].concat();
} |
|
I think this looks ok. |
…x.d.ts (DefinitelyTyped#17157) * Creating a separate interface for IncomingMessageHeaders This allows for IncomingMessage.headers types to be extensible per project. Suppose a node service expects a fixed set of headers of certain type (say string), that service can specify custom IncomingMessageHeaders with more concrete types defined. For example ``` interface IncomingMessageHeaders { "host": string; "set-cookie": string[] } ``` * Changing IncomingMessageHeaders defn This definition adds undefined in addition to string and string[].
This allows for IncomingMessage.headers types to be extensible per project. Suppose a node service expects a fixed set of headers of certain type (say string), that service can specify custom IncomingMessageHeaders with more concrete types defined.
For example
Please fill in this template.
npm run lint package-name(ortscif notslint.jsonis present).Select one of these and delete the others:
If changing an existing definition: