-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Contextual typing failure with discriminated type and partially overlapping type. #16513
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
Comments
Hi,
It's part of the description of the bug. If interface Bar is declared with
its property 'foo' of type 'ThatType', then the compiler has no issues. So
this works fine:
type MyType = 'meep'|'bidi';
type ThatType = 'moop'|'meep';
interface Foo {
isfoo: 'true';
foo: MyType;
}
interface Bar {
isfoo: 'false';
foo: ThatType;
}
let arr: Array<Foo|Bar>;
arr = [{foo: 'meep', isfoo: 'true'},
{foo: 'moop', isfoo: 'false'}];
But if ThatType = string; then the compile error comes back.
…On 14 June 2017 at 03:15, devdoomari ***@***.***> wrote:
just a small question, but how is 'ThatType' being used for?
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#16513 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAa5RRAAJ8_8Km5IwA0GsLLdCyrktYQ7ks5sD4iTgaJpZM4N5WGr>
.
|
The first symptom we're seeing is that What we think is happening is that when looking at the type of the |
Similarly ran into this issue when typing responses from a database. I'm aware that the current solution is to employ discriminated type unions with varying string values. interface T {
_id: string
revision: number
}
interface Error {
_id: undefined
error: string
}
const getDoc = (s: string): T | Error => {
if (Math.random() < 0.5) {
const r: T = {
_id: "123",
revision: 1
}
return r
}
else {
const e: Error = {
error: "Not found"
}
// Error: property 'name' is missing in type { error: string }
return e
}
}
const doc = (id: string) => {
const d = getDoc(id)
if (d._id === undefined) {
console.log(d.error)
// 'error' doesnt exist on type 'T'
}
else {
console.log(d.revision)
// Error: 'revision' doesnt exist on type 'Error'
}
} |
Duplicate of #16457 |
TypeScript Version: 2.3 +
Code
Expected behavior: This should compile. The discriminant
isfoo
should be enough to figure out that the first element of the array can only be a Foo and the second is a Bar.Actual behavior:
ERROR(15,1): : Type '({ foo: string; isfoo: "true"; } | { foo: string; isfoo: "false"; })[]' is not assignable to type '(Foo | Bar)[]'.
Type '{ foo: string; isfoo: "true"; } | { foo: string; isfoo: "false"; }' is not assignable to type 'Foo | Bar'.
Type '{ foo: string; isfoo: "true"; }' is not assignable to type 'Foo | Bar'.
Type '{ foo: string; isfoo: "true"; }' is not assignable to type 'Foo'.
Types of property 'foo' are incompatible.
Type 'string' is not assignable to type 'MyType'.
However, if Foo's
foo
is declared as astring
, then it compiles fine. If Bar'sfoo
is declared as aThatType
, then it also compiles fine.The text was updated successfully, but these errors were encountered: