Skip to content

Commit

Permalink
Adding test
Browse files Browse the repository at this point in the history
  • Loading branch information
ahejlsberg committed Jan 30, 2015
1 parent c7e7bb1 commit 0056760
Show file tree
Hide file tree
Showing 3 changed files with 117 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
//// [typeArgumentInferenceWithObjectLiteral.ts]
interface Computed<T> {
read(): T;
write(value: T);
}

function foo<T>(x: Computed<T>) { }

var s: string;

// Calls below should infer string for T and then assign that type to the value parameter
foo({
read: () => s,
write: value => s = value
});
foo({
write: value => s = value,
read: () => s
});


//// [typeArgumentInferenceWithObjectLiteral.js]
function foo(x) {
}
var s;
// Calls below should infer string for T and then assign that type to the value parameter
foo({
read: function () { return s; },
write: function (value) { return s = value; }
});
foo({
write: function (value) { return s = value; },
read: function () { return s; }
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
=== tests/cases/conformance/expressions/functionCalls/typeArgumentInferenceWithObjectLiteral.ts ===
interface Computed<T> {
>Computed : Computed<T>
>T : T

read(): T;
>read : () => T
>T : T

write(value: T);
>write : (value: T) => any
>value : T
>T : T
}

function foo<T>(x: Computed<T>) { }
>foo : <T>(x: Computed<T>) => void
>T : T
>x : Computed<T>
>Computed : Computed<T>
>T : T

var s: string;
>s : string

// Calls below should infer string for T and then assign that type to the value parameter
foo({
>foo({ read: () => s, write: value => s = value}) : void
>foo : <T>(x: Computed<T>) => void
>{ read: () => s, write: value => s = value} : { read: () => string; write: (value: string) => string; }

read: () => s,
>read : () => string
>() => s : () => string
>s : string

write: value => s = value
>write : (value: string) => string
>value => s = value : (value: string) => string
>value : string
>s = value : string
>s : string
>value : string

});
foo({
>foo({ write: value => s = value, read: () => s}) : void
>foo : <T>(x: Computed<T>) => void
>{ write: value => s = value, read: () => s} : { write: (value: string) => string; read: () => string; }

write: value => s = value,
>write : (value: string) => string
>value => s = value : (value: string) => string
>value : string
>s = value : string
>s : string
>value : string

read: () => s
>read : () => string
>() => s : () => string
>s : string

});

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
interface Computed<T> {
read(): T;
write(value: T);
}

function foo<T>(x: Computed<T>) { }

var s: string;

// Calls below should infer string for T and then assign that type to the value parameter
foo({
read: () => s,
write: value => s = value
});
foo({
write: value => s = value,
read: () => s
});

3 comments on commit 0056760

@JsonFreeman
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a test for the parentheses too, since you fixed that as well.

@JsonFreeman
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, can you add a test with multiple parameters, to test that all the parameters are visited first (without context sensitive), and then the context sensitive ones are revisited? Namely, if the first parameter is context sensitive, and the second is not, you want to infer from both, and then visit the context sensitive part of the first one.

Actually one further test. You infer a type from the context insensitive parts of each argument, and then you decide that the signature is not applicable so you never get to the context sensitive part.

@DanielRosenwasser
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seconded, these are good tests to have.

Please sign in to comment.