-
Notifications
You must be signed in to change notification settings - Fork 12.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Interface with methods interferes with string literal type inference #19497
Comments
We've had some issues about string literal types in the past but this seems like a new one: declare function id<T>(val: T): T
declare function arr<T>(val: T): T[]
const x: "a" = id("a");
const y: "a"[] = arr("a");
|
This is a design limitation and is working as intended. See #10676 for context. In the original example we can't tell that |
@andy-ms Your example is working as it should. Array elements are mutable locations so widening is to be expected. |
This doesn't seem to be affected by mutability? declare function arr<T>(val: T): ReadonlyArray<T>
const y: ReadonlyArray<"a"> = arr("a"); // Error
|
@wolfgang42 Another potential workaround for your example if you are only expecting strings is: interface Observable<T extends string> {peek(): T}
declare function obs<T extends string>(val: T): Observable<T>
const t: Observable<'A'> = obs('A') // now works |
@kpdonn The Observable in my example was actually from a library, so I can't change its declaration. Hopefully someone else will find that useful, though. [I've since had to rewrite the code that had this issue for other reasons, this is not currently a problem for me any more.] |
Another case: declare function createArray<T>(): Array<T>;
interface I {
a: number;
b: true[];
}
const x: I = { a: 0, b: createArray() };
|
Automatically closing this issue for housekeeping purposes. The issue labels indicate that it is unactionable at the moment or has already been addressed. |
TypeScript Version: 2.7.0-dev.20171026
Minimal example:
Expected behavior:
Compilation without errors.
Actual behavior:
This error goes away if the interface has no methods or function signature:
Alternately, you can redundantly specify the type of the argument to
obs()
:The text was updated successfully, but these errors were encountered: