3

It is possible in typescript somehow make possibly empty interface incompatible with string?

interface IA {
    foo?:number;
}

function bar(arg:IA) {
}

bar("should not compile");

Playground link

Added later:

More complex example with possible workaround which is limiting in different way (you can extend only classes or interfaces):

interface IACommon {
    common?:number;
    f1?:string;
    f2?:any;
}

interface IAWithF1 extends IACommon {
    f1:string;
}

interface IAWithF2 extends IACommon {
    f2:any;
}

type IA = IAWithF1 | IAWithF2;

function bar(arg:IA) {
}

bar("does not compile but next definition also does not compile");

interface IAExtended extends IA {
    ext?: any;
}

Playground link

In this I found only workaround to extend IAExtended from IACommon but that makes IAExtended also not protected against this bug with passing string instead of object.

5
  • No. It follows from this PR github.com/Microsoft/TypeScript/pull/3823 as far as I remember. Commented Jan 8, 2016 at 9:48
  • Can you convert your code to use class instead of interface as a workaround? Commented Jan 8, 2016 at 9:55
  • No it needs to be just object, but there are multiple members and only some combinations of members are allowed so possibly I can create union type ... Commented Jan 8, 2016 at 9:57
  • Yes, maybe. Please add more details if you want a useful answer for this question. Otherwise simple no is the correct answer ;) Commented Jan 8, 2016 at 10:24
  • Added workaround. I will accept "No" answer after one day :-) Commented Jan 8, 2016 at 10:59

3 Answers 3

2

New feature of TS 2.2 allows this nice solution of problem:

interface IACore {
    foo?: number;
}

type IA = IACore & object;

function bar(arg:IA) {
}

bar("does not compile in TS 2.2.");
Sign up to request clarification or add additional context in comments.

Comments

1

You could make your interface a dictionary type, by setting the type of the key:

interface IA {
    [key: string]: any;
    foo?:number;
}

function bar(arg:IA) {
}

bar("wont compile");

Playground link

1 Comment

Interesting. Of course this does not help me at all because it allows huge amount of another bugs. But it answers my original question.
0

I think I have a better answer here (in case anybody comes looking). I had a similar situation, but I won't bother showing that. Let's go back to your original case. A found a solution with only a slight change.

My version generates an error (at least as of TS 2.1.4):

interface IA {
    foo: number | undefined;
}

function bar(arg:IA) {
}

bar("should not compile");

The only semantic difference I can see here is you actually have to always provide a value for foo in all object literals e.g.,

bar({}); // Error
bar({ foo: undefined }); // OK
bar({} as IA); // Also OK

You can find some discussion on this point here.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.