You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
From a type perspective, functions are a type of object, which can have properties. You are assigning a function to an object with no required properties. Therefore, a function with no properties is assignable to an interface with all optional properties.
Why all the cases you have in your playground example was because TypeScript did not detect weak types properly, and as of 2.4 it does. That means that strings and numbers are no longer assignable but these weak types.
I tried to use object & Partial<T> as a type to avoid functions being assignable, but I am afraid functions are still considered subtypes of objects by the type system. But I might be missing something...
interfaceFoo{cat: stringbar: number[key: string]: string|number|undefined}typeBar<Baz>=|Partial<Baz>|(()=>Partial<Baz>)constnotBar: Bar<Foo>=()=>{}// fails on 2.4.1 and 2.5.0-dev.20170629 constalsoNotBar: Bar<Foo>='test'// fails on 2.4.1constalsoAlsoNotBar: Bar<Foo>=false// fails on 2.4.1
just updating for anyone who comes across this via google.
on 2.4.1 the following works and retains autocomplete.
interface StylesComplete {
propA: 'letter a' | 'a',
propB: 'letter b' | 'b',
}
// A Partial on an interface will normally accept arbitrary functions
// due to partials accepting empty objects which validate to anything in
// typescript
const doesNotFail: Partial<StylesComplete> = () => { }
interface StylesLossy {
[key: string]: string | undefined
}
type Styles =
& StylesComplete
& StylesLossy
// intersection of
// * an interface containing random props
// * index signature interface
// allows you to do a partial which will successfully fail on arbitrary
// functions.
// successfully fails
const doesFail: Partial<Styles> = () => { }
Activity
[-]Bug: Partials accept functions[/-][+]Partials accept functions[/+]kitsonk commentedon Jul 7, 2017
Addressed in TypeScript FAQs.
luke-john commentedon Jul 7, 2017
Apologies but it's not clear to me why a function is assignable to a partial interface based on that.
My issue is not to do with the parameters on the function, but that a function is assignable to a partial interface
kitsonk commentedon Jul 7, 2017
Ah, I see the challenge...
From a type perspective, functions are a type of object, which can have properties. You are assigning a function to an object with no required properties. Therefore, a function with no properties is assignable to an interface with all optional properties.
Why all the cases you have in your playground example was because TypeScript did not detect weak types properly, and as of 2.4 it does. That means that strings and numbers are no longer assignable but these weak types.
I tried to use
object & Partial<T>as a type to avoid functions being assignable, but I am afraid functions are still considered subtypes of objects by the type system. But I might be missing something...luke-john commentedon Jul 8, 2017
Edit: Upon trying the below unfortunately it does not work due to an issue with unions
So after sleeping on it, I think I can restructure my use of Partial to achieve what I want with the following.
typescript-playground-example
Cheers for looking into it
luke-john commentedon Jul 10, 2017
Adding an index signature to the interface you pass to the
Partialprevents it accepting functions.typescript playground example
edit: note, this does kill autocomplete though :/
luke-john commentedon Jul 14, 2017
just updating for anyone who comes across this via google.
on 2.4.1 the following works and retains autocomplete.
typescript-playground-example