@@ -61,9 +61,7 @@ declare namespace io {
6161 filename ?: string ,
6262 ...formats : T
6363 ) : LuaIterable <
64- LuaMultiReturn <
65- [ ] extends T ? [ string ] : { [ P in keyof T ] : T [ P ] extends 'n' ? number : string }
66- >
64+ LuaMultiReturn < [ ] extends T ? [ string ] : { [ P in keyof T ] : FileReadFormatToType < T [ P ] > } >
6765 > ;
6866
6967 /**
@@ -100,12 +98,31 @@ declare namespace io {
10098 * you can use to read data from this program (if mode is "r", the default) or
10199 * to write data to this program (if mode is "w").
102100 */
103- function popen ( prog : string , mode ?: 'r' | 'w' ) : LuaFile ;
101+ function popen ( prog : string , mode ?: 'r' | 'w' ) : LuaMultiReturn < [ LuaFile ] | [ undefined , string ] > ;
104102
105103 /**
106104 * Equivalent to io.input():read(···).
107105 */
108- const read : LuaFile [ 'read' ] ;
106+ function read ( ) : io . FileReadFormatToType < io . FileReadLineFormat > | undefined ;
107+ function read < T extends io . FileReadFormat > ( format : T ) : io . FileReadFormatToType < T > | undefined ;
108+ function read < T extends io . FileReadFormat [ ] > (
109+ ...formats : T
110+ ) : LuaMultiReturn < { [ P in keyof T ] ?: io . FileReadFormatToType < T [ P ] > } > ;
111+
112+ /**
113+ * Predefined file handle for standard error stream. The I/O library never closes this file.
114+ */
115+ const stderr : LuaFile ;
116+
117+ /**
118+ * Predefined file handle for standard input stream. The I/O library never closes this file.
119+ */
120+ const stdin : LuaFile ;
121+
122+ /**
123+ * Predefined file handle for standard output stream. The I/O library never closes this file.
124+ */
125+ const stdout : LuaFile ;
109126
110127 /**
111128 * In case of success, returns a handle for a temporary file. This file is
@@ -125,6 +142,8 @@ declare namespace io {
125142 * Equivalent to io.output():write(···).
126143 */
127144 function write ( ...args : ( string | number ) [ ] ) : LuaMultiReturn < [ LuaFile ] | [ undefined , string ] > ;
145+
146+ type FileReadFormatToType < T > = T extends FileReadNumberFormat ? number : string ;
128147}
129148
130149interface LuaFile {
@@ -157,12 +176,10 @@ interface LuaFile {
157176 * In case of errors this function raises the error, instead of returning an
158177 * error code.
159178 */
160- lines < T extends FileReadFormat [ ] > (
179+ lines < T extends io . FileReadFormat [ ] > (
161180 ...formats : T
162181 ) : LuaIterable <
163- LuaMultiReturn <
164- [ ] extends T ? [ string ] : { [ P in keyof T ] : T [ P ] extends 'n' ? number : string }
165- >
182+ LuaMultiReturn < [ ] extends T ? [ string ] : { [ P in keyof T ] : io . FileReadFormatToType < T [ P ] > } >
166183 > ;
167184
168185 /**
@@ -193,9 +210,11 @@ interface LuaFile {
193210 *
194211 * The formats "l" and "L" should be used only for text files.
195212 */
196- read < T extends FileReadFormat [ ] > (
213+ read ( ) : io . FileReadFormatToType < io . FileReadLineFormat > | undefined ;
214+ read < T extends io . FileReadFormat > ( format : T ) : io . FileReadFormatToType < T > | undefined ;
215+ read < T extends io . FileReadFormat [ ] > (
197216 ...formats : T
198- ) : LuaMultiReturn < { [ P in keyof T ] ?: T [ P ] extends 'n' ? number : string } > ;
217+ ) : LuaMultiReturn < { [ P in keyof T ] ?: io . FileReadFormatToType < T [ P ] > } > ;
199218
200219 /**
201220 * Sets and geionts the file position, measured from the beginning of the
0 commit comments